0
votes

I have a WPF ComboBox created in XAML:

        <ComboBox DockPanel.Dock="Top" Background="{DynamicResource Esri_BackgroundPressedBrush}" DataContext="DockpaneData" 
            Foreground="{DynamicResource Esri_TextMenuBrush}" HorizontalAlignment="Stretch" FontSize="14" VerticalAlignment="Center" Margin="5,0,5,0" IsEditable="True" Text="Jump To"
            ItemsSource="{Binding FeatureEditUpdates}">
        </ComboBox>

I set the DataContext of the ComboBox "DockpaneData", which in this case is an internal class called "DockpaneData" within another file called DockpaneViewModel.cs.

Why Can't I bind to the public class property "FeatureEditUpdates"?

It will work if I put it in the class with the same name as the file, and DON'T set the datacontext in the ComboBox. Why?

The error I'm getting in the output window is:

System.Windows.Data Error: 40 : BindingExpression path error: 'FeatureEditUpdates' property not found on 'object' ''String' (HashCode=-1567065443)'. BindingExpression:Path=FeatureEditUpdates; DataItem='String' (HashCode=-1567065443); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

Where I see it thinks the datacontext is a string, which doesn't make sense to me.

Thank you!

1
Datacontext is inherited from it's parent, so you don't need to set datacontext of individual controls, according to the exception, datacontext of your combox is of string type, not of DockPaneViewModel - Athul Raj

1 Answers

0
votes

The expression

<ComboBox ... DataContext="DockpaneData"/>

sets the DataContext of the ComboBox to the string literal "DockpaneData", which you see in the error message

property not found on 'object' ''String'

One way to set the DataContext correctly is to declare and use a XAML resource:

<Window.Resources>
    <local:DockpaneData x:Key="DockpaneData"/>
</Window.Resources>
...
<ComboBox ... DataContext="{StaticResource DockpaneData}"/>