我创建了简单DataGridTemplateColumn
的包含组合框。我为Visibility
整列设置了绑定,也为ItemSource
和设置SelectedItem
了绑定ComboBox
(我需要ItemsSource
为不同的行设置不同的绑定)。一切正常,直到我隐藏该列。之后(并再次显示),它们ComboBoxes
是空的,但 getterItemsSource
和SelectedItem
binding 返回良好的值。当我调用绑定的 setter 时SelectedItem
,以前的值是正确的,并且新值显示在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
隐藏(和显示)列后重置的屏幕截图。我知道隐藏是问题所在,因为感叹号显示在隐藏之后:
任何想法?谢谢。