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

2 に答える 2

21

マージンのためにローカルスタイルに削除する必要があると思います

    <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>
于 2013-06-28T05:50:37.613 に答える
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;
}
于 2013-06-28T04:40:17.897 に答える