Access Converted ItemsSource Objects in Custom User Control Code Behind

I have a question regarding accessing ItemsSource items that have been converted through an IValueConverter. I have an ItemsSource Dependency Property on a customer user control like so:

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

This is bound to a Combo Box in my user control using the following XAML:

        <ComboBox Name="cbObjects" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="0">
                        <CheckBox Content="{Binding ObjectData}" IsChecked="{Binding IsSelected}"
                                  HorizontalAlignment="Stretch" VerticalAlignment="Center" Checked="OnCbObjectCheckBoxChecked" Unchecked="OnCbObjectCheckBoxChecked" />
                <MultiBinding Converter="{StaticResource ObjectToSelectableObjectConverter}">
                    <Binding Path="ItemsSource" ElementName="ThisControl" />
                    <Binding Path="ItemsSource.Count" ElementName="ThisControl"/>

As you can see, I’m using an ObectToSelectableObject converter on the ItemsSource of the ComboBox. This converter takes a IEnumerable<T> and converts them into an IEnumerable<SelectableObject<T>> where SelectableObject<T> contains the properties bound to the checkbox in the DataTemplate (ObjectData, IsSelected).

All of my items show up just fine in my Combo Box and the converter converts the T objects into SelectableObject<T> with no issue. However, when I try and access the ItemsSource in the code behind, I see that ItemsSource is still of type IEnumerable<T> instead of IEnumerable<SelectableObject<T>> So I’m unable to set the IsSelected property of these items from code behind like I want.

Is there any way, after converting ItemsSource from another object, to access the new object list that was created by the converter? Any help would be greatly appreciated.

—Edit for more information —

I’m accessing ItemsSource from the code behind in the following manner:

        protected void CheckBox_Checked(object sender, RoutedEventArgs e)
           foreach(var item in ItemsSource)
                Type t = item.GetType();

Type t end up having the value of the underlying object data that was passed in and not the SelectableObject<T> I’m hoping to see.

Leave a Reply

Your email address will not be published. Required fields are marked *