数据触发器绑定到视图模型属性

11

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

        <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个回答

22

我认为你需要移除本地样式以解决边距问题。

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

太好了,谢谢!我认为两个答案的结合帮助了我,我之前忘记先包含更改通知,因为我之前尝试过删除本地边距属性,但它没有起作用。所以通过实施这两个答案,我解决了问题,谢谢! - MikeDub

1

您可能会错过属性更改的通知。请确认您的视图模型是否实现了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;
}

1
我已更新问题,包括更多关于我的解决方案/问题的信息,这确实实现了它。 - MikeDub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接