0
votes

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;
}
2
Is Name a property defined in Location class? Post code for that as well.Rohit Vats
Get rid of the <StackPanel> element, it should just be a textbox inside of itemtemplateKcvin
Rohit, posted, and yes; Netscape, done, same problem though.John
You have IsHitTestVisible and IsEnabled set to false. It doesn't seem like you would be able to open the ComboBox.evanb
@John - As stated before Name should be a property and not field. Convert field to property and it will work fine.Rohit Vats

2 Answers

2
votes

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; }
}
1
votes

You should be binding to properties and not variables. Instead of

public string Name;

You should change the above to

public string Name {get;set;}

Even better, can implement the INotifyPropertyChanged..