1

ドメイン モデルに poco クラスがあります。

public class Slot
{
    bool HasPlayed { get; set; }
}

リストボックス項目テンプレートに表示しています。

<Border Background="...">
    <CheckBox IsChecked="{Binding Path=HasPlayed, Mode=TwoWay}" />
</Border>

しかし、私がやりたいのは、HasPlayed が true の場合、境界線の背景色が赤に変わり、false の場合は緑になります。これらのブラシはリソース ディクショナリにあります。

ドメイン モデルにブラシを追加することもできますが、それでは懸念事項の分離が無効になります。また、今後チェックボックスを使用するつもりはありません。これは単なる UI のモックアップです。

IValueConverter を試しましたが、プロパティが変更されても変更されません。モデルは INotifyPropertyChanged を実装します。

プロパティが変更された場合、どのように色を変更しますか?

4

3 に答える 3

1

DataTrigger を使用してアクションをトリガーできます。

http://en.csharp-online.net/WPF_Styles_and_Control_Templates%E2%80%94Data_Triggers

于 2011-10-15T23:20:18.330 に答える
0

SlotがINotifyPropertyChangedを実装していないため、プロパティの変更が取得されていないと思います。

次のようなものを試してください。

public class Slot : INotifyPropertyChanged
    {
        private bool _hasPlayed;

        private bool HasPlayed
        {
            get { return _hasPlayed; }
            set
            {
                _hasPlayed = value;
                InvokePropertyChanged(new PropertyChangedEventArgs("HasPlayed"));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public void InvokePropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }
    }
于 2011-10-15T23:21:23.477 に答える
0

コンバーターに渡す値は のように見えるSlotため、バインディングが更新されるためには、バインディングは変更されるプロパティを指していませんが、バインディングは関連するプロパティ (この場合は ) を指すパスを指定する必要がありますHasPlayed。(もちろん、プロパティを所有するオブジェクトはINPCを実装する必要がありますが、すでにそうであるとおっしゃいましたよね?)

于 2011-10-15T23:30:25.050 に答える