Reputation: 283273
I've got a window with a field like this:
public partial class VariablesWindow : Window
{
public Dictionary<string, string> Variables { get; private set; }
And then in the window's XAML, I've created a ListView. How do I set the ItemsSource to bind to Variables
?
<ListView ItemsSource="???"
Upvotes: 1
Views: 523
Reputation: 437734
<ListView ItemsSource="{Binding Path=Variables}" />
However, you may find it more useful to bind directly to keys or values, which you would do like this:
<ListView ItemsSource="{Binding Path=Variables.Values}" />
Update: These bindings will work as long as your DataContext
is null
or equal to this
. Otherwise, you would need to use something more cumbersome, like
<ListView ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=x:Type Window}}, Path=Variables}
For binding help, take a look at this superb cheat sheet.
Upvotes: 1
Reputation: 137188
I think the following should work:
<ListView ItemsSource="{Binding Path=Variables}" />
I'm not in a position to double check this right now, so I'm not 100% of whether the Path=
is needed or not.
There's more information on the binding syntax on the MSDN here and here. All their examples have the Path=
syntax.
And you will have to set the DataContext
.
Upvotes: 1
Reputation: 283273
This works, but it's a bit ridiculously verbose:
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:VariablesWindow, AncestorLevel=1}
Doesn't seem like the best way to do it.
Upvotes: 0
Reputation: 15173
I think what ChrisF posted would work...
<ListView ItemsSource="{Binding Variables}" />
But you may need to explicity set the DataContext.
this.DataContext = this;
Upvotes: 4