通过代码设置条件属性时,不会在 wpf 应用程序中设置按钮背景颜色
本文关键字:设置 应用程序 wpf 按钮 颜色 背景 代码 条件 属性 | 更新日期: 2023-09-27 18:36:07
我的一个用户控件中有一个按钮,其背景需要根据属性集进行更改。 无论设置什么属性的值(真或假),背景颜色都不会更改。我什至在调试过程中注意到属性的值设置为 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
}
你不需要DataTrigger
,因为你没有改变它设置器的任何内容。您还必须提高PropertyChanged
Background
也与RiskHighLowMedium
public bool RiskHighLowMedium
{
get { return _riskHighLowMedium; }
set
{
_riskHighLowMedium = value;
this.OnPropertyChanged("RiskHighLowMedium");
this.OnPropertyChanged("Background12");
}
}
在 XAML 中
<Button Name="highLowRisk" Padding="5" Grid.Column="2" Width="30" Height="30" Background={Binding Background12} Margin="5,5,5,5" />
希望这对!!有所帮助
public partial class MainWindow : Window
{
private ExampleViewModel m_ViewModel;
public MainWindow()
{
InitializeComponent();
m_ViewModel = new ExampleViewModel();
this.DataContext = m_ViewModel;
}
private void btn_Click(object sender, RoutedEventArgs e)
{
m_ViewModel.RiskHighLowMedium = true;
}
}
public class ExampleViewModel : INotifyPropertyChanged
{
private bool _riskHighLowMedium = false;
public ExampleViewModel()
{
}
public bool RiskHighLowMedium
{
get
{
return _riskHighLowMedium;
}
set
{
_riskHighLowMedium = value;
OnPropertyChanged("RiskHighLowMedium");
}
}
public Brush Background
{
get
{
return RiskHighLowMedium ? Brushes.Red : Brushes.Blue;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
<Style x:Key="CircleButtonStyle" TargetType="Button">
<Setter Property="Background" Value="{Binding Path=Background, Mode=OneWay}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RiskHighLowMedium}" Value="True">
<Setter Property="Background" Value="{ Binding Path= Background, Mode=OneWay}"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Grid>
<Button Name="btn" Height="40" Width="180" Content="Hello" Style="{StaticResource CircleButtonStyle}" Click="btn_Click"></Button>
</Grid>