Reputation: 4984
I have a DataGrid binded to an array and with columns generated at runtime based on the size o the array:
ObservableCollection<DisplayByte[]> items = new ObservableCollection<DisplayByte[]>();
// List is populated
...
...
Style style = new Style(typeof(TextBlock));
Setter setter = new Setter(TextBlock.ForegroundProperty, Brushes.LightGreen);
DataTrigger trigger = new DataTrigger() { Binding = new Binding("IsEqual"), Value = true };
trigger.Setters.Add(setter);
style.Triggers.Add(trigger);
dgBlobViewer.Columns.Add( new DataGridTextColumn
{
Header = "",
Binding = new Binding(string.Format("[{0}].Value", columnIndex++)),
ElementStyle = style
});
The DisplayByte class is this:
public class DisplayByte : INotifyPropertyChanged
{
private bool m_isequal;
public DisplayByte(string value)
{
Value = value;
IsEqual = false;
}
public String Value
{
get;
set;
}
public Boolean IsEqual
{
get
{
return m_isequal;
}
set
{
m_isequal = value;
OnPropertyChanged(new PropertyChangedEventArgs("IsEqual"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
}
The binding is working properly, so I'm displaying on the datagrid cells the value of the Value Property. I want to style the cells, so their backgound is changed if the IsEqual Property is true, but it's not working. All the cells are always white.
The XAML of the Datagrid is this:
<DataGrid x:Name="dgBlobViewer"
Grid.Row="0"
Grid.RowSpan="1"
Margin="10,10,10,0"
ItemsSource="{Binding}"
AutoGenerateColumns="False"
FontFamily="Consolas" >
Is there anything wrong with the binding or the style?
Upvotes: 0
Views: 82
Reputation: 63387
The IsEqual
property is of your data item, so you have to use DataTrigger
in this case:
var trigger = new DataTrigger();
trigger.Binding = new Binding("[index].IsEqual");//index is placeholder.
trigger.Value = true;
trigger.Setters.Add(setter);
style.Triggers.Add(trigger);
Upvotes: 1