I have got a button in one of my usercontrols whose background needs to be changed based on a property set. no matter what the value of Property is set (true or false), background color is never changed. I even noticed during debugging that value of property is set to true but the trigger is not fired to set the background color.
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.SubToolbar"
x:Name="SubToolBar" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
<UserControl.Resources>
<Style x:Key="MyFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Control}">
<Grid Margin="8">
<Ellipse Name="r1" Stroke="Black" StrokeDashArray="2 2" StrokeThickness="1"/>
<Border Name="border" Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight}" BorderThickness="1" CornerRadius="2"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="CircleButton" TargetType="Button">
<Setter Property="Background" Value="{ Binding Path= Background12, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Mode=OneWay}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RiskHighLowMedium }" Value="True">
<Setter Property="Background" Value="{ Binding Path= Background12, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Mode=OneWay}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Border Background="#FFD4BFAE" CornerRadius="5" >
<Grid x:Name="LayoutRoot" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Padding="5" Text="{Binding Path=ID}"/>
<TextBox Grid.Column="1" Margin="2,0,0,0" Padding="5" Text="{Binding Path=Name}"/>
<Button Name="highLowRisk" Padding="5" Grid.Column="2" Width="30" Height="30" Style="{StaticResource CircleButton}" Margin="5,5,5,5" />
</Grid>
</Border>
I am setting the style template like this in the xaml file.
but nothing seems to change the background color.
the code below is
/// <summary>
/// Interaction logic for SubToolbar.xaml
/// </summary>
public partial class SubToolbar : UserControl, INotifyPropertyChanged
{
/// <summary>
/// default ctor
/// </summary>
public SubToolbar()
{
DataContext = this;
InitializeComponent();
}
/// <summary>
/// setting whethere
/// risk is high or low
/// </summary>
/// <param name="_riskhighLow"></param>
public void SetRiskHighLow(bool _riskhighLow)
{
_riskHighLowMedium = _riskhighLow;
this.OnPropertyChanged("RiskHighLowMedium");
this.OnPropertyChanged("Background12");
}
/// <summary>
/// RiskHighLowMedium
/// </summary>
private bool _riskHighLowMedium;
/// <summary>
/// property to set risk high
/// low and medium
/// </summary>
public bool RiskHighLowMedium
{
get { return _riskHighLowMedium; }
set
{
_riskHighLowMedium = value;
this.OnPropertyChanged("RiskHighLowMedium");
this.OnPropertyChanged("Background12");
}
}
/// <summary>
/// background property
/// to set button background
/// color
/// </summary>
public Brush Background12
{
get
{
return RiskHighLowMedium ? Brushes.Red : Brushes.Blue;
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion // INotifyPropertyChanged Members
}