4

DataGrid の CellTemplate 用に 2 つのテンプレートがあります。アイテムを変更すると、テンプレートを選択するのに役立ちません。私の DisplayModeTemplateSelector は呼び出されません!

アイテムが変更されたときにこの CellTemplateSelector を再度トリガーする方法があるかどうか疑問に思っているのは? コンテンツが変更されたときに DataGrid または ListView で CellTemplate を更新する方法

<DataGridTemplateColumn x:Name="colorRange"
                        Width="*"
                        Header="Color Range">
    <DataGridTemplateColumn.CellTemplateSelector>
        <local:DisplayModeTemplateSelector HeatMapTemplate="{StaticResource heatMapTemplate}" ThreshHoldTemplate="{StaticResource threshHoldTemplate}" />
    </DataGridTemplateColumn.CellTemplateSelector>
</DataGridTemplateColumn>

このブログを見つけました http://dotdotnet.blogspot.com/2008/11/refresh-celltemplate-in-listview-when.html

これは私の問題と似ていると思いますが、本当に彼を理解できません! 誰でも説明できますか?

4

2 に答える 2

4

クラスがビジュアル ツリーに属していないため、ブログ投稿の解決策はDataGridコントロールでDataGridTemplateColumnは機能しません。静的クラスにバインドしようとしても、プロパティの変更後に奇妙な例外が発生したため、成功しませんでした。

とにかく、この問題を解決するには2つの方法があります。

1) より簡単な方法。

ObservableCollectionクラスの使用。

    var itemIndex = 0;
    var currentItem = vm.Items[itemIndex];
    //Change necessary properties
    //..
    vm.Items.Remove(currentItem);
    vm.Items.Insert(itemIndex, currentItem);      

2) より複雑な方法。

オブジェクト自体を返すプロパティを項目クラスに追加できます。

    public ItemViewModel(/*...*/)
    {
        this.SelfProperty = this;
        //...
    }

    public ItemViewModel SelfProperty { get; private set; }

    public void Update()
    {
        this.SelfProperty = null;
        this.OnPropertyChanged("SelfProperty");
        this.SelfProperty = this;
        this.OnPropertyChanged("SelfProperty");
    }

その後、次のように のContentControl.ContentTemplateSelector代わりに を使用できます。CellTemplateSelector

            <DataGridTemplateColumn Header="Color Range">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ContentControl Content="{Binding SelfProperty}"  ContentTemplateSelector="{StaticResource mySelector}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

プロパティを変更するときは、Update何らかの方法でメソッドを呼び出します。

  currentItem.SomeDataProperty = "some new value";
  //Or you can add this method call to the OnPropertyChanged 
  //so that it calls authomatically
  currentItem.Update(); 

SelfProperty最初にUpdateメソッドでnull 値を設定した理由は、プロパティが完全に変更さSelectorれるまでテンプレートを更新しないためです。Content同じオブジェクトをもう一度設定すると、何も起こりませんが、最初に null 値を設定すると、変更が処理されます。

于 2011-09-25T10:34:31.153 に答える
2

簡単な方法は、コンボ ボックスの Selection Changed イベントをフックし、テンプレート セレクターを再割り当てすることです。これにより、リフレッシュが強制されます。

XAML の場合 (DataGrid/ComboBoxColumn の残りの部分は次のようになります。

<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
    <Setter Property="ItemsSource" 
            Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},  Path=DataContext.Gates, UpdateSourceTrigger=PropertyChanged}"/>
    <EventSetter Event="SelectionChanged" Handler="GateIDChanged" />
</Style>

これは、この DataGridTemplateColumn を参照します。

<DataGridTemplateColumn x:Name="GateParamsColumn" Header="Gate Parameters" CellTemplateSelector="{StaticResource GateParamsTemplateSelector}"></DataGridTemplateColumn>

そしてコードビハインドで:

private void GateIDChanged(object sender, SelectionChangedEventArgs eventArgs)
{
    var selector = GateParamsColumn.CellTemplateSelector;
    GateParamsColumn.CellTemplateSelector = null;
    GateParamsColumn.CellTemplateSelector = selector;
}
于 2012-08-15T17:01:25.457 に答える