2

私が理解しているように、DataTriggers は WinRT ではサポートされていません。しかし、これを回避するにはどうすればよいでしょうか。

INotifyPropertyChanged を拡張して、「選択済み」プロパティが実装されたモデルがあります。Selected が True の場合、ボタンの境界線を赤に変更したいと思います。プロパティの変更を状態の変更のトリガーにリンクする方法がわかりません。

モデル

class MyObject : INotifyPropertyChanged
{
    public MyObject()
    {
        Selected = true;
    }

    private bool _selected;

    public bool Selected
    {
        get { return _selected; }
        set { _selected = value; OnPropertyChanged("Selected"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

XAML

<Grid>
    <Button Margin="131,97,171,124" Content="Hello!" DataContext="MyObject" d:DataContext="{d:DesignInstance local:MyObject}">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Border BorderThickness="1">
                    <Border.BorderBrush>
                        <SolidColorBrush Color="Aqua"/>
                    </Border.BorderBrush>
                    <StackPanel>
                        <TextBlock Text="{TemplateBinding Content}"/>
                        <TextBlock Text="{Binding Selected}"/>
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Button.Template>
    </Button>
</Grid>
4

2 に答える 2

1

簡単なコンバーター ( IValueConverter) を使用できます。次に、ボーダー プロパティをSelectedViewModel のプロパティにバインドします。

<Border BorderBrush="{Binding Selected, Converter={StaticResource MyConverter}}">

もちろん、ページ リソースまたはアプリ リソース ディクショナリのどこかにリソースを配置する必要があります。

<mynamespace:MyConverter x:Key="MyConverter"/>

次に、コンバーターで、値が であるかどうかを確認しTrue、赤いカラー ブラシを返します。

(私はこれを簡単に書きましたが、テストはしていませんが、これを簡単に行う方法をグーグルで検索できます)

于 2012-10-25T21:55:47.607 に答える
0

あなたが見るかもしれないポートがあります。 https://nuget.org/packages/Windows.UI.Interactivity

https://github.com/jlaanstra/Windows.UI.Interactivity

于 2012-10-27T06:21:08.410 に答える