0

我的一个用户控件中有一个按钮,其背景需要根据属性集进行更改。无论 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




}
4

2 回答 2

2

你不需要DataTrigger在这里,因为你没有改变它的任何东西。你也必须PropertyChanged加注BackgroundRiskHighLowMedium

        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" />
于 2013-10-21T08:52:14.567 回答
0

希望这可以帮助 !!!

  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>
于 2013-10-21T09:13:36.403 回答