我的一个用户控件中有一个按钮,其背景需要根据属性集进行更改。无论 Property 的值是什么(true 或 false),背景颜色都不会改变。我什至在调试过程中注意到属性的值设置为 true,但没有触发触发器来设置背景颜色。
<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>
我在 xaml 文件中设置这样的样式模板。
但似乎没有任何改变背景颜色。
下面的代码是
/// <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
}