datatrigger绑定到viewmodel属性

时间:2013-06-28 04:20:02

标签: wpf mvvm datatrigger

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

        <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 属性的按钮时,我仍然无法正常工作?

我已经实现了这个属性:

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

这是实现更改控制的方法

    public virtual void OnPropertyChanged(string propertyInfo)
    {
        App.Current.Dispatcher.BeginInvoke((Action)(() =>
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyInfo));
            }
        }
        ));
    }

2 个答案:

答案 0 :(得分:16)

我认为你必须删除本地风格的保证金

    <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>

答案 1 :(得分:1)

您可能会错过财产变更中的通知。请确认您的viewmodel是否实现了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;
}