【问题标题】:ComboBox in DataGridTemplateColumn is cleared after hide the column in WPF隐藏 WPF 中的列后清除 DataGridTemplateColumn 中的组合框
【发布时间】:2018-07-22 07:08:58
【问题描述】:

我创建了包含组合框的简单DataGridTemplateColumn。我为整列的Visibility 以及ComboBoxItemSourceSelectedItem 设置了绑定(我需要为不同的行设置不同的ItemsSource)。一切正常,直到我隐藏该列。之后(并再次显示),ComboBoxes 为空,但 ItemsSourceSelectedItem 绑定的 getter 返回良好的值。当我调用SelectedItem 绑定的setter 时,之前的值是正确的,ComboBox 中会显示一个新值。所以一切都是正确的,但是为什么在隐藏列之后组合框被重置,即使ViewModel 中的数据也是正确的并且没有任何改变:

  <DataGridTemplateColumn Header="Total" Visibility="{Binding ProxyData.ReportConfigurationVM.ShowTotalRow, Source={StaticResource BindingProxy}, Converter={StaticResource BoolToVisibilityConverter}}">
     <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
           <ComboBox Width="70"
                     ItemsSource="{Binding Path=TotalsAggregationFunctions}"
                     SelectedItem="{Binding Path=SelectedTotalAggregation, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
           </ComboBox>
        </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
  </DataGridTemplateColumn>

我的视图模型:

  public enum AggregationFunction
  {
     None,
     Sum,
     Avg
  }

  private AggregationFunction selectedTotalAggregation;
  public AggregationFunction SelectedTotalAggregation
  {
     get { return this.selectedTotalAggregation; }
     set { SetField(ref this.selectedTotalAggregation, value); }  // this calls OnPropertyNotify automatically
  }

  public IEnumerable<AggregationFunction> TotalsAggregationFunctions
  {
     get
     {
        // no matter what I return, nothing works when hide the column...
        return new AggregationFunction[] { AggregationFunction.None, AggregationFunction.Sum, AggregationFunction.Avg, AggregationFunction.Min, AggregationFunction.Max };
     }
  }

隐藏(和显示)列后重置的ComboBoxes 的屏幕截图。我知道隐藏是问题所在,因为感叹号显示在隐藏之后:

有什么想法吗?谢谢。

【问题讨论】:

    标签: wpf combobox reset datagridtemplatecolumn itemsource


    【解决方案1】:

    我发现上述错误发生在组合框的 ItemsSoure 是不可为空对象(int、enum 等)的集合的情况下。如果 ItemsSource 是一个可为空的对象(字符串、类),则隐藏/显示包含组合框的列可以正常工作(如预期的那样)。

    因此,您需要使用可为空的 Enum 类型(Enum?),然后一切正常。解决方法是将 Enum 转换为 String 或将 Enum 包装到一个类中。

    public enum AggregationFunction
    {
       None,
       Sum,
       Avg
    }
    
    private AggregationFunction? selectedTotalAggregation;
    public AggregationFunction? SelectedTotalAggregation
    {
       get { return this.selectedTotalAggregation; }
       set { SetField(ref this.selectedTotalAggregation, value); }  // this calls OnPropertyNotify automatically
    }
    
    public IEnumerable<AggregationFunction?> TotalsAggregationFunctions
    {
       get
       {
          // no matter what I return, nothing works when hide the column...
          return new AggregationFunction?[] { AggregationFunction.None, AggregationFunction.Sum, AggregationFunction.Avg, AggregationFunction.Min, AggregationFunction.Max };
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 2013-02-28
      相关资源
      最近更新 更多