datatrigger binding to viewmodel property

21,323

Solution 1

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>

Solution 2

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;
}
Share:
21,323
MikeDub
Author by

MikeDub

Microsoft® Certified Solutions Developer (MCSD): Web Applications MCSD: App Builder (Charter Member) MCSA: Web Applications (Charter Member) MCP: Microsoft Certified Professional MS: Programming in HTML5 with JavaScript and CSS3 ASP.NET MVC Web Applications Developer Microsoft Azure and Web Services Developer Nuix Secure Development Trained in OWASP Top 10

Updated on May 18, 2020

Comments

  • MikeDub
    MikeDub almost 4 years

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