Reputation: 87
I`ve got such an issue: my datatrigger doesnt change color after binded data changes. Datatrigger is on TextBlock which is ElemntTenplate for ItemsControl.
My Xaml:
<Grid>
<Grid.RowDefinitions>
<RowDefinition ></RowDefinition>
<RowDefinition Height="3*"></RowDefinition>
</Grid.RowDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition ></RowDefinition>
<RowDefinition ></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding TaresToReturn, StringFormat='К возврату: {0}'}"></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding TaresReturned, StringFormat='Возвращено: {0}'}"></TextBlock>
</Grid>
<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Visible">
<ItemsControl ItemsSource="{Binding Tares}" Margin="5">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="tareReturnController:p_GetTaresForReturnResult">
<Border Width="100" Height="30" Margin="10">
<TextBlock x:Name="TextBlock" Text="{Binding barcode}" FontSize="15">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding IsReturned, UpdateSourceTrigger=PropertyChanged}" Value="false">
<Setter Property="TextBlock.Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsReturned, UpdateSourceTrigger=PropertyChanged}" Value="true">
<Setter Property="TextBlock.Background" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Border>
<!--<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsReturned}" Value="false">
<Setter TargetName="TextBlock" Property="TextBlock.Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsReturned}" Value="true">
<Setter TargetName="TextBlock" Property="TextBlock.Background" Value="Green"/>
</DataTrigger>
</DataTemplate.Triggers>-->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
My ViewModel parts:
private ObservableCollection<p_GetTaresForReturnResult> tares_;
public ObservableCollection<p_GetTaresForReturnResult> Tares
{ get { return tares_; } set { tares_ = value; } }
private void GetTares()
{
tares_ = new ObservableCollection<p_GetTaresForReturnResult>(Terminal.Dc.p_GetTaresForReturn(DateTime.Parse("05.05.2015")));
}
public int TaresToReturn
{ get { return Tares.Count(t => Convert.ToBoolean(t.IsReturned) == false); } }
public int TaresReturned
{ get { return Tares.Count(t => Convert.ToBoolean(t.IsReturned)); } }
public void ScannerOnOnBarcodeReceived(string[] _args)
{
foreach (string barcode in _args)
{
var tt = Tares.FirstOrDefault(t => t.barcode.ToLower().Trim() == barcode.ToLower().Trim());
if (tt != null)
{
var ttt = new p_GetTaresForReturnResult();
ttt.barcode = "111";
ttt.id = 111;
ttt.IsReturned = false;
tares_.Add(ttt);//Add new element for test
tt.IsReturned = true;
tt.barcode = "1";
RaisePropertyChanged("TaresToReturn");
RaisePropertyChanged("TaresReturned");
RaisePropertyChanged("barcode");
RaisePropertyChanged("IsReturned");
RaisePropertyChanged("Tares");
}
}
}
Code of p_GetTaresForReturnResult
public partial class p_GetTaresForReturnResult
{
private int _id;
private string _barcode;
private System.Nullable<bool> _IsReturned;
public p_GetTaresForReturnResult()
{
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_id", DbType="Int NOT NULL")]
public int id
{
get
{
return this._id;
}
set
{
if ((this._id != value))
{
this._id = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_barcode", DbType="NChar(20)")]
public string barcode
{
get
{
return this._barcode;
}
set
{
if ((this._barcode != value))
{
this._barcode = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_IsReturned", DbType="Bit")]
public System.Nullable<bool> IsReturned
{
get
{
return this._IsReturned;
}
set
{
if ((this._IsReturned != value))
{
this._IsReturned = value;
}
}
}
}
When page is loaded first time, my elements are coloured right. When i change the collection and call RaisePropertyCahnged they dont change their color! Notice: if i add new elemnt to collection it appears at WrapPanel. But items that was already created dont change their color and text!
Can anyone tell me what I`m doing wrong?
Upvotes: 1
Views: 1304
Reputation: 1344
If you want to handle this smoothly, I think you have to wrap this class with a NotificatonObject
which contains a p_GetTaresForReturnResult
field and writes/reads property from this, but it calls RaisePropertyChanged
in setters.
Otherwise you need to replace items to Notify View about changes.
Edited by Messenger: I modified p_GetTaresForReturnResult class:
public partial class p_GetTaresForReturnResult : INotifyPropertyChanged
{
...
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_IsReturned", DbType="Bit")]
public System.Nullable<bool> IsReturned
{
get
{
return this._IsReturned;
}
set
{
if ((this._IsReturned != value))
{
this._IsReturned = value;
NotifyPropertyChanged("IsReturned");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Upvotes: 1