数据触发器绑定到属性

本文关键字:属性 绑定 触发器 数据 | 更新日期: 2023-09-27 17:49:27

我试图改变控件的外观时,属性的值发生变化。我正在使用下面的XAML和c#代码,但是当我单击改变属性myProperty值的按钮时,除了第一次运行应用程序外,什么也没有发生。有什么建议吗?

 <Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Height="34" HorizontalAlignment="Left" x:Name="toggleBroadcast" VerticalAlignment="Top" Width="133" Margin="180,0,0,0">
            <Button.Style>
                <Style x:Name="bb" TargetType="{x:Type Button}">
                    <Setter Property="Content" Value="Original Content"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=myProperty}" Value="true">
                            <Setter Property="Content" Value="IS TRUE"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=myProperty}" Value="false">
                            <Setter Property="Content" Value="IS FALSE"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
         </Button>
        <Button Content="Click on This Button" HorizontalAlignment="Left" Width="158" Click="Button_Click_1" Height="34" VerticalAlignment="Top"/>
   </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3
{
    public partial class MainWindow : Window
    {
        Boolean _myProperty = false;
        public MainWindow()
        {
            DataContext = this;
            InitializeComponent();
        }
        public Boolean myProperty
        {
            get
            {
                return _myProperty;
            }
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            _myProperty = !_myProperty;
        }
    }
}

数据触发器绑定到属性

如果你不使用DependencyProperty,你将不得不实现INotifyPropertyChanged,这样它可以通知Xaml的属性已经改变

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private bool _myProperty = false;
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    public bool myProperty
    {
        get { return _myProperty; }
        set { _myProperty = value; NotifyPropertyChanged("myProperty"); }
    }
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        myProperty = !myProperty;
    }
    public event PropertyChangedEventHandler PropertyChanged;
    /// <summary>
    /// Notifies the property changed.
    /// </summary>
    /// <param name="property">The property name that changed.</param>
    public void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

或使用DependencyProperty

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }
    public bool myProperty
    {
        get { return (bool)GetValue(myPropertyProperty); }
        set { SetValue(myPropertyProperty, value); }
    }
    // Using a DependencyProperty as the backing store for myProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty myPropertyProperty =
        DependencyProperty.Register("myProperty", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(false));

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        myProperty = !myProperty;
    }
}