When I searched for indexed property binding in WPF, I did find some useful information like this one:
If I have a binding like so in XAML:
Text="{Binding [5], Mode=OneWay}"
Then, with this, if my datacontext has an integer indexer this[int indx] then I would get the element at index 5.
And if my datacontext has an indexer this[string sindx] (and no integer indexer), then I will get the element indexed by "5".
So far so good.
But, if I want to bind to a multidimensional indexed property, then it took a bit of "research" to figure out that I can do it by: Text="{Binding [5\,1], Mode=OneWay}"
Good to know... {Binding [2\,hello], Mode=OneWay} in the example below translates to:
XAML to test indexer binding:
Then, with this, if my datacontext has an integer indexer this[int indx] then I would get the element at index 5.
And if my datacontext has an indexer this[string sindx] (and no integer indexer), then I will get the element indexed by "5".
So far so good.
But, if I want to bind to a multidimensional indexed property, then it took a bit of "research" to figure out that I can do it by: Text="{Binding [5\,1], Mode=OneWay}"
Good to know... {Binding [2\,hello], Mode=OneWay} in the example below translates to:
Binding b = new Binding("[2,hello]");
b.Mode = BindingMode.OneWay;
textBlock5.SetBinding(TextBox.TextProperty, b);
XAML to test indexer binding:
<TextBox Name="textBlock1" Text="{Binding [5], Mode=OneWay}" />
<TextBox Name="textBlock2" Text="{Binding [some\,string],Mode=OneWay}" />
<TextBox Name="textBlock3" Text="{Binding (Name), Mode=OneWay,RelativeSource={x:Static RelativeSource.Self }}" />
<TextBox Name="textBlock4" Text="{Binding SomeProp, Mode=OneWay}" />
<TextBox Name="textBlock5" Text="{Binding [2\,hello], Mode=OneWay}" />
<TextBox Name="textBlock6" Text="{Binding [Jagged][Jagged indexer property], Mode=OneWay}"/>
ViewModel:
<TextBox Name="textBlock2" Text="{Binding [some\,string],Mode=OneWay}" />
<TextBox Name="textBlock3" Text="{Binding (Name), Mode=OneWay,RelativeSource={x:Static RelativeSource.Self }}" />
<TextBox Name="textBlock4" Text="{Binding SomeProp, Mode=OneWay}" />
<TextBox Name="textBlock5" Text="{Binding [2\,hello], Mode=OneWay}" />
<TextBox Name="textBlock6" Text="{Binding [Jagged][Jagged indexer property], Mode=OneWay}"/>
ViewModel:
public class TestVM : INotifyPropertyChanged { public string this[int i] { get { return "Int single dimensional indexer: " + i.ToString(); } } public string this[string s1, string s2] { get{ return "String Multi Dimensional Indexer Called: " + s1 + " , " + s2; } } public string this[int i1, string s1] { get{ return "Integer String Multi Dimensional Indexer Called: " + i1.ToString() + " , " + s1; } } public string SomeProp { get{ return "SomeProp: Hello";} } public object this[string s] { get { if (s == "Jagged") return this;//Just being lazy: test accessing something as [][] when the initial indexer is "Jagged" else return "String single dimensional indexer: " + s; } } public event PropertyChangedEventHandler PropertyChanged; public void Notify(string propertyname) {if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyname)); } }
No comments:
Post a Comment