I'm trying to create a simple style data trigger that pulls it's binding value from a viewmodel property, as you can see below:
<StackPanel Name="stackTextPanel" Orientation="Horizontal" Margin="0,8,0,0">
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Style.Triggers>
<DataTrigger Binding="{Binding QuickDrawBarPinned}" Value="False">
<Setter Property="Margin" Value="0,8,0,0" />
</DataTrigger>
<DataTrigger Binding="{Binding QuickDrawBarPinned}" Value="True">
<Setter Property="Margin" Value="0,48,0,0" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
I have also tried the variant
Binding="{Binding Path=QuickDrawBarPinned}"
but this is still not working when I press the button that changes the QuickDrawBarPinned property what am I doing wrong?
I've implemented the property as so:
private bool _quickDrawBarPinned = false;
/// <summary>
/// Indicates if the Quick Draw Bar is pinned (stuck) or unpinned (retractable)
/// </summary>
public bool QuickDrawBarPinned
{
get { return _quickDrawBarPinned; }
set
{
_quickDrawBarPinned = value;
OnPropertyChanged("QuickDrawBarPinned");
}
}
This is the method that implements the change control
public virtual void OnPropertyChanged(string propertyInfo)
{
App.Current.Dispatcher.BeginInvoke((Action)(() =>
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyInfo));
}
}
));
}
I think you have to remove to local style for your margin
<StackPanel Name="stackTextPanel" Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Setter Property="Margin" Value="0,8,0,0" />
<Style.Triggers>
<DataTrigger Binding="{Binding QuickDrawBarPinned}" Value="False">
<Setter Property="Margin" Value="0,8,0,0" />
</DataTrigger>
<DataTrigger Binding="{Binding QuickDrawBarPinned}" Value="True">
<Setter Property="Margin" Value="0,48,0,0" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
You may miss the Notification in property change. Please confirm whether your viewmodel implement the INotifyPropertyChanged,
public class ViewModel : INotifyPropertyChanged
{
private bool quickDrawBarPinned;
public bool QuickDrawBarPinned
{
get { return quickDrawBarPinned; }
set { quickDrawBarPinned = value; RaisePropertyChanged("QuickDrawBarPinned"); }
}
public void RaisePropertyChanged(string propertyname)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With