How to use my custom property as a trigger?
I have a class (Compound) binding to DataGrid. I want to change background color of a cell when a property is set to true. Here is what I tried:
public class Compound : DependencyObject
{
public static readonly DependencyProperty RSquaredFlagProperty =
DependencyProperty.Register("RSquaredFlag", typeof(bool),
typeof(Compound), new FrameworkPropertyMetadata(false));
public bool RSquaredFlag
{
get { return (bool)GetValue(RSquaredFlagProperty); }
set { SetValue(RSquaredFlagProperty, value); }
}
...
}
XAML:
<common:DataGridEx ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Header="R^2" Binding="{Binding RSquared, StringFormat=N3}">
<DataGridTextColumn.ElementStyle>
<Style>
<Style.Triggers>
<Trigger Property="model:Compound.RSquaredFlag" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</common:DataGridEx>
However, with this code, I get a compiler error "Cannot resolve the Style Property 'Background'开发者_开发百科. Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the Property".
What did I miss? How to make it work?
Change it to Property="TextBlock.Background"
or specify a respective TargetType
in your style.
I don't think the trigger will work by the way since it will look for the property on the control itself not its DataContext, use a DataTrigger
instead.
精彩评论