2

我正在尝试根据单击按钮时更改其值的 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>
4

1 回答 1

4

首先,您的类必须实现INotifyPropertyChanged( msdn ) 以通知视图您的属性已更改。

其次,您必须DataContextMainWindow构造函数中分配。

例子:

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 。

于 2013-07-08T16:29:22.683 回答