简单的DataTrigger不工作。为什么?

时间:2013-07-08 16:15:11

标签: c# wpf data-binding triggers datatrigger

我正在尝试根据bool属性更改文本块的前景色,以便在单击按钮时更改其值。但是,出于某种原因,这是行不通的。另外,我是否必须首先将bool属性添加到列表中?我尝试将bool属性直接添加到DataContext,但这也不起作用。任何帮助将不胜感激。

    public static bool IsOn { get; set; }
    public static List<bool> boo;
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        IsOn = true;
        boo = new List<bool>();
        boo.Add(IsOn); 
        DataContext = boo;                   
    }
<Window.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="Green" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsOn}" Value="true">
                <Setter Property="Foreground" Value="Red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <StackPanel>
        <Button Click="Button_Click" Content="Change Color" />
        <TextBlock Name="textBlockColor" Text="My Foreground Color" />
    </StackPanel>

更新: 在kmatyaszek的答案运作良好之后,我发现以下代码也有效,简短而且重点:

    public bool IsOn { get; set;}
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        IsOn = true;
        textBlockColor.DataContext = this;
    }

UPDATE2 : 正如Viv在评论中建议的那样,我通过创建一个单独的类来尝试他的方法,并且以下代码工作正常:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();  
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Test test = new Test();
        test.IsOn = true;
        textBlockColor.DataContext = test;
    }    
}

public class Test 
{
    public bool IsOn { get; set; }
}

UPDATE3 以下方法与Viv建议的方法一样有效。在这种情况下,属性被添加到列表中,我还添加了第二个属性用于测试。但是,这种方法需要了解与使用的索引相关的属性:

public partial class MainWindow : Window
{
    public List<bool> boo;
    public bool IsOn { get; set; }
    public bool IsBlue { get; set; }
    public MainWindow()
    {
        InitializeComponent();  
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        IsOn = true;
        IsGreen = true;
        boo = new List<bool>();
        boo.Add(IsOn);
        boo.Add(IsGreen);
        DataContext = boo;
    }
}

XAML

 <Window.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="Green" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding [1]}" Value="true">
                <Setter Property="Foreground" Value="Blue" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
<StackPanel>
    <Button Click="Button_Click" Content="Change Color" />
    <TextBlock Name="textBlockColor" Text="My Foreground Color" />
</StackPanel>

UPDATE4 以下代码也适用,使用Dictionary而不是List允许使用您选择的名称标识每个项目:

public partial class MainWindow : Window
{
    public Dictionary<string,bool> booDictionary;
    public bool IsBlue { get; set; }
    public MainWindow()
    {
        InitializeComponent();  
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        IsBlue = true;
        booDictionary = new Dictionary<string,bool>();
        booDictionary.Add("Blue",IsBlue);
        DataContext = booDictionary;
    }
}

XAML

<Window.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="Green" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding [Blue]}" Value="true">
                <Setter Property="Foreground" Value="Blue" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <StackPanel>
        <Button Click="Button_Click" Content="Ok" />
        <TextBlock Name="textBlockColor" Text="Hey" />
    </StackPanel>

1 个答案:

答案 0 :(得分:4)

首先,您的班级必须实施INotifyPropertyChangedmsdn)以通知视图您的媒体资源已被更改。

其次,您必须在DataContext构造函数中指定MainWindow

示例:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = this;
    }

    private bool _isOn;
    public bool IsOn
    {
        get { return _isOn; }
        set { _isOn = value; OnPropertyChanged("IsOn"); }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        IsOn = !IsOn;          
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propName));
    }
}

您的XAML代码没问题。

如果您的绑定不起作用,您将来应该使用snoop