2012-12-24 240 views
2

我想在属性值更改时更改控件的外观。我使用下面的XAML和C#代码,但是当我单击更改Property myProperty的值的按钮时,除了第一次运行应用程序时,没有任何反应。有什么建议么?DataTrigger绑定到属​​性

<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; 
     } 
    } 
} 

回答

3

如果你不使用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; 
    } 
} 
+1

感谢sa_ddam213解决提供的代码,然后建议一个更合适的替代方案很好的答案! – user1925920