2

あなたの助けが必要です。アプリケーションで MVVM デザイン パターンを使用しています。データグリッド内にコンボボックスがあります:

<telerik:RadGridView AutoGenerateColumns="False" Name="rgvData" ItemsSource="{Binding Data}">
        <telerik:RadGridView.Columns>
            <telerik:GridViewColumn  Header="Department">
                <telerik:GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <telerik:RadComboBox
                                            ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.Departments}"
                                            SelectedValue="{Binding DEP_LINK,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                            SelectedValuePath="LINK"
                                            DisplayMemberPath="TITLE">
                        </telerik:RadComboBox>
                    </DataTemplate>
                </telerik:GridViewColumn.CellTemplate>
            </telerik:GridViewColumn>
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>

データは DATA の ObservableCollection です。Departments は DEPARTMENTS の ObservableCollection です。どちらのクラスも SQL Server のエンティティです。

ここで、DATA クラスの IsDepartmentUsed プロパティに基づいてコンボボックスに色を付けたいと思います。そのためのスタイルは次のとおりです。

<telerik:RadComboBox.Style>
   <Style TargetType="{x:Type telerik:RadComboBox}">
       <Style.Triggers>
           <DataTrigger Binding="{Binding IsDepartmentUsed}" Value="False">
               <Setter Property="Background" Value="Red"/>
           </DataTrigger>
           <DataTrigger Binding="{Binding IsDepartmentUsed}" Value="True">
               <Setter Property="Background" Value="Transparent"/>
           </DataTrigger>
       </Style.Triggers>
   </Style>
</telerik:RadComboBox.Style>

ウィンドウを開いたばかりのときは問題なく動作します。しかし、コンボボックスの値を変更しても、色は変わりません!

IsDepartmentUsed プロパティは次のようになります。

public partial class DATA
{
    public bool IsDepartmentUsed
    {
        get
        {
            if (this.DEPARTMENTS.ISUSED != null)
            {
                return Convert.ToBoolean(this.DEPARTMENTS.ISUSED);
            }
            return false;
        }
    }
}
4

1 に答える 1

2

UI は、プロパティが変更されたことを認識していないため、更新する必要があることを認識していません。プロパティが変更されるたびに、PropertyChangedイベント ハンドラーをトリガーする必要があります。IsDepartmentUsedIsUsed

Entity Framework のようなものを使用してモデルを生成しているように聞こえるので、データ階層を正しく理解していれば、部分クラスはコレクションにイベント ハンドラーをDataアタッチできます。これにより、内部のクラスにイベント ハンドラーが追加または削除されます。これにより、プロパティが変更されるたびに変更通知が発生します。CollectionChangedDepartmentsPropertyChangeDepartmentIsDepartmentUsedIsUsed

void Data()
{
    this.Departments.CollectionChanged += Departments_CollectionChanged;
}

void Departments_CollectionChanged (object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.NewItems != null)
    {
        foreach(Department item in e.NewItems)
            item.PropertyChanged += Department_PropertyChanged;
    }
    if (e.OldItems != null)
    {
        foreach(Department item in e.OldItems)
            item.PropertyChanged -= Department_PropertyChanged;
    }
}

void Department_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "IsUsed")
        RaisePropertyChanged("IsDepartmentUsed");
}

ここでは、レイヤーに使用しているため、DepartmentクラスがINotifyPropertyChangedを実装していると想定していますModel

(あなたのコードは、あなたが言うのは少し難しいですDEPARTMENTSが、 anObservableCollectionにはC# コードにあるようなプロパティObservableCollectionがないISUSEDため、データ階層を誤解している可能性があります。ただし、同じ概念が適用されるはずです - に何かを添付する必要があります変更のたびに通知PropertyChangedを発生させるクラスのイベント)PropertyChangedIsDepartmentUsedIsUsed

于 2013-01-24T17:13:30.257 に答える