Simple databinding question

I'm trying to set up some simple databinding with the January CTP and I'm having some trouble. Here's my code:

<Window x:Class="BlahBlah.Window1"
xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
Title="RssAggregator" Height="800" Width="600" Loaded="loaded">
<Grid Name="mainGrid" DataContext="{x:Null}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.75*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<ListBox Name="list" Grid.Column="0" Grid.Row="0" Margin="0,0,5,0" ItemsSource="{Binding Path=Channels}" />
<ListView Grid.Column="1" Grid.Row="0" Margin="0,0,5,0" ItemsSource="{Binding Path=Channels/Items}" IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header=" " />
<GridViewColumn Header="Name" DisplayMemberPath="Title" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
<TextBox Grid.Column="2" Grid.Row="0" Text="{Binding Path=Channels/Items/Description}" />
</Grid>
</Window>

and on the code-behind:

private void loaded(object sender, EventArgs e)
{
Test test = new Test();
test.Channels = new List<Channel>();

test.Channels.Add(Aggregator.RecieveChannel(new Uri(@"http://scobleizer.wordpress.com/feed/")));

mainGrid.DataContext = test.Channels;
}

Anyway, the Grid's DataContext is set, but the child controls aren't picking up on it. I realize that there won't be any formatting, but that's step two.

What am I doing wrong I'm almost positive I've done this exact same thing before and all the examples online basically say to do this.

Thanks


Answer this question

Simple databinding question

  • pschuur

    Drew's comment is correct, but there is more to do. You need to use a collection that supports binding notifications. List<> does not do this. ObservableCollection<> does this and is basically the same as List<> otherwise. Also, use "." not "/" for the path separator when working with objects. "/" may work for XMLDataSource and related XSLT type work, but I do not believe it works in this case.

  • Erik Sorensen

    I think the problem is you're setting the Grid's DataContext to test.Channels when you really want to set it to test because your ListBox has a binding to Channels. I can't really be 100% sure though 'cause I have no clue what a Test looks like from an interface standpoint.

    HTH,
    Drew


  • ChrisMcV

    I figured it out.

    First, just to clarify, thanks for pointing out the test.Channels. I changed the code to that after I had tried simply binding to the test instance to try it out.

    Anyway, the problem was that the class Test was exposing the List<Channel> as a public field, rather than a property. As you can see with the name, it was a simple placeholder class, and I didn't feel like writing the extra lines. I didn't think it would pose a problem.

    As for the issue with List<> vs ObservableCollection<>, you can use List<>, but your application will not automatically update when the collection changes. An example is hooking up the TextChanged event of my textbox with code that adds a new item to the test.Channels. If the list is declared as List<>, the item is added, but you don't see it. Changing it to an ObservableCollection<> automatically updates it.

    Thanks for everyone's input!

  • Simple databinding question