4

我有 2 个用于 DataGrid 的 CellTemplate 的模板。当我更改项目时,它不会帮助我为我选择模板,我的 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该类不属于可视化树,甚至当我尝试将其绑定到静态类时,由于属性更改后出现奇怪的异常,我也没有成功。

无论如何,有两种可能的方法来解决这个问题。

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(); 

我首先SelfPropertyUpdate方法中设置空值的原因是,在属性完全更改Selector之前不会更新模板。Content如果我再次设置同一个对象 - 什么都不会发生,但如果我先为其设置一个空值 - 将处理更改。

于 2011-09-25T10:34:31.153 回答
2

简单的方法是挂钩组合框的选择更改事件,并重新分配模板选择器。这会强制刷新。

在 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 回答