Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

wpf - datatrigger binding to viewmodel property

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));
            }
        }
        ));
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...