John John -4 years ago 99
C# Question

WPF Combobox with ItemsSource - What path do I set to access this databinding?

I'm having a problem setting the Binding/Path property in my XAML.

I know this ComboBox's

ItemSource
property is updating properly, since I get a bunch of empty text-boxes when I update the
viewmodel
(instead of textboxes with text, which is what I expect).

So I believe the Binding in the
DataTemplate
section of my ComboBox needs a different binding path, but I'm not sure what to set the binding path to.

<ComboBox ItemsSource="{Binding Path=Locations}" Visibility="{Binding SettingsOptionsVisibility}" Grid.Column="0" x:Name="locationCB" VerticalAlignment="Top" SelectionChanged="locationCB_SelectionChanged" HorizontalAlignment="Left" Width="350" Height="30" IsHitTestVisible="False" IsEnabled="False" Focusable="False">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>


Here's a picture of my LocationCB's ItemsSource in the Watch List in my Codebehind: http://imgur.com/x4SYWER

As expected, my combobox is populated with 8 (textless) elements. What do I need to do to get my binding the text to Name to connect up?

EDIT: code for the Locations object in the ViewModel:

public ObservableCollection<Location> Locations { get; set; }


And code for the Location class wrapped in the observable collection, as requested:

public class Location
{
public Guid LocationID;
public Guid ParentID;
public String Name;
public bool isValid;
}

Answer Source

Change the fields of your location object to properties:

public class Location
{
    public Guid LocationID { get; set; }
    public Guid ParentID { get; set; }
    public String Name { get; set; }
    public bool isValid { get; set; }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download