数据触发器绑定到视图模型属性 [英] datatrigger binding to viewmodel property

查看:18
本文介绍了数据触发器绑定到视图模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的样式数据触发器,从视图模型属性中提取其绑定值,如下所示:

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>

这篇关于数据触发器绑定到视图模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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