0

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: https://i.stack.imgur.com/EkiHU.jpg

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;
}
cchamberlain
  • 17,444
  • 7
  • 59
  • 72
John
  • 565
  • 1
  • 6
  • 18

2 Answers2

2

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; }
}
Michael Gunter
  • 12,528
  • 1
  • 24
  • 58
  • 1
    You probably want to make `Location` implement `INotifyPropertyChanged` as well – evanb Apr 02 '14 at 18:33
  • Fields and properties are different things. The `get;` and `set;` syntax creates a property with a default backing field. WPF does not bind to fields. – Michael Gunter Apr 02 '14 at 19:03
1

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..

TDaddy Hobz
  • 454
  • 4
  • 11