【发布时间】:2018-07-22 07:08:58
【问题描述】:
我创建了包含组合框的简单DataGridTemplateColumn。我为整列的Visibility 以及ComboBox 的ItemSource 和SelectedItem 设置了绑定(我需要为不同的行设置不同的ItemsSource)。一切正常,直到我隐藏该列。之后(并再次显示),ComboBoxes 为空,但 ItemsSource 和 SelectedItem 绑定的 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