|
I am trying to change the appearance of a control when the value of a property changes. I am using the XAML and C# code below, but nothing happens when I click on the button that changes the value of the Property myProperty, except when I first run the application. Any suggestions?
<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 _Check = false;
public MainWindow()
{
DataContext = this;
InitializeComponent();
}
public Boolean check
{
get
{
return _Check;
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
_Check = !_Check;
}
}
}
|
|
|
|
|
There are a couple of things wrong with your code here. The first thing is that you don't have to supply both true and false DataTrigger elements. Add one of those triggers as the default content. When the bool value goes to the default value, the style trigger is removed.
Now, as to why you aren't seeing the value change. Even though you are changing the value, you aren't telling the XAML that the value has changed. Make this class implement INotifyPropertyChanged and have your code raise the PropertyChanged event when the vale changes. This raises the change notification.
Oh, and you aren't binding to check in your example. You are binding to myProperty.
|
|
|
|
|
First of all, you have not done any binding to your Check Property, binding to myProperty will not help you at all. You need to change your binding to bind to the Check property. Secondly for bindings to react to changes in the bound property you need to implement the INotifyPropertyChanged[^] interface in you viewmodel/code behind. Try this code :
<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=Check}" Value="true">
<Setter Property="Content" Value="IS TRUE" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Check}" 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>
public partial class MainWindow : Window,INotifyPropertyChanged
{
Boolean _check = false;
public MainWindow()
{
DataContext = this;
InitializeComponent();
}
public Boolean Check
{
get
{
return _check;
}
set
{
if (_check != value)
{
_check = value;
OnPropertyChanged("Check");
}
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Check = !Check;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|