Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf datatrigger绑定到viewmodel属性_Wpf_Mvvm_Datatrigger - Fatal编程技术网

Wpf datatrigger绑定到viewmodel属性

Wpf datatrigger绑定到viewmodel属性,wpf,mvvm,datatrigger,Wpf,Mvvm,Datatrigger,我试图创建一个简单的样式数据触发器,从viewmodel属性中提取其绑定值,如下所示: <StackPanel Name="stackTextPanel" Orientation="Horizontal" Margin="0,8,0,0"> <StackPanel.Style> <Style TargetType="{x:Type StackPanel}">

我试图创建一个简单的样式数据触发器,从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>
但是,当我按下更改QuickDrucarPinned属性的按钮时,这仍然不起作用。我做错了什么

我实现了如下属性:

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

您可能会错过属性更改中的通知。请确认您的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;
}

我认为你必须改用当地风格来赚取利润

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


我已更新了问题,以包含有关我的解决方案/问题的更多信息,该解决方案/问题确实实现了这一点。答对了,谢谢!我认为这两个答案的结合帮助了我,我忘记了首先包括变更通知,因为我以前尝试删除本地保证金属性,但它不起作用。因此,通过实现这两个答案,我解决了这个问题,谢谢!
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;
}
    <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>