datatrigger绑定到viewmodel属性 [英] datatrigger binding to viewmodel property

查看:96
本文介绍了datatrigger绑定到viewmodel属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的样式数据触发器,该触发器将其绑定值从viewmodel属性中拉出,如下所示:

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>

我也尝试过变种

Binding="{Binding Path=QuickDrawBarPinned}"

但是当我按下更改 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>

这篇关于datatrigger绑定到viewmodel属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆