我对访问通过IValueConverter转换的ItemsSource项目有疑问。我在像这样的自定义用户控件上有一个ItemsSource依赖项属性:
public IEnumerable ItemsSource
{
get { return (IEnumerable)Getvalue(ItemsSourceProperty); }
set { Setvalue(ItemsSourceProperty,value); }
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource",typeof(IEnumerable),typeof(ComboboxWithCheckboxes),new PropertyMetadata(null,null));
这使用以下XAML绑定到我的用户控件中的组合框:
<ComboBox Name="cbObjects" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="0">
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding ObjectData}" IsChecked="{Binding IsSelected}"
HorizontalAlignment="Stretch" VerticalAlignment="Center" Checked="OnCbObjectCheckBoxChecked" Unchecked="OnCbObjectCheckBoxChecked" />
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemsSource>
<MultiBinding Converter="{StaticResource ObjectToSelectableObjectConverter}">
<Binding Path="ItemsSource" ElementName="ThisControl" />
<Binding Path="ItemsSource.Count" ElementName="ThisControl"/>
</MultiBinding>
</ComboBox.ItemsSource>
</ComboBox>
如您所见,我在ComboBox的ItemsSource上使用了ObectToSelectableObject
转换器。该转换器将IEnumerable<T>
转换为IEnumerable<SelectableObject<T>>
,其中SelectableObject<T>
包含绑定到DataTemplate(ObjectData,IsSelected)中复选框的属性。
我的所有项目都很好地显示在了“组合框”中,转换器将T
对象转换为SelectableObject<T>
没有问题。但是,当我尝试在后面的代码中访问ItemsSource时,我看到ItemsSource的类型仍然是IEnumerable<T>
而不是IEnumerable<SelectableObject<T>>
,因此我无法从后面的代码中设置这些项目的IsSelected属性如我所愿。
从另一个对象转换ItemsSource之后,有什么方法可以访问由转换器创建的新对象列表吗?任何帮助将不胜感激。
---编辑以获得更多信息---
我正在通过以下方式从后面的代码访问ItemsSource:
protected void CheckBox_Checked(object sender,RoutedEventArgs e)
{
foreach(var item in ItemsSource)
{
Type t = item.GetType();
}
}
Type t
最终具有传入的基础对象数据的值,而不是我希望看到的SelectableObject<T>
。