In WPF, Listbox can hold IEnumerable while ContentPresneter can display CurrentItem. When both control share the same DataContext, Master detail relationship is established:
public class Stocks : List
{
public Stocks()
{
Add(new Stock("YHOO", 27.90));
Add(new Stock("MSFT", 28.90));
Add(new Stock("GOOG", 179.40));
}
}
public class Stock {
public string Symbol { get; set; }
public double Price { get; set; }
public Stock(string s, double p) {
this.Price = p; this.Symbol = s; }
public override string ToString() { ..}
}
<Window x:Class="MasterDetailWPF.Window1"
xmlns="..."
xmlns:x="..."
...
xmlns:local="clr-namespace:MasterDetailWPF">
<Window.DataContext>
<local:Stocks>
</Window.DataContext>
<Grid>
<ListBox ..
ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True">
</ListBox>
<ContentPresenter Content="{Binding}" ..>
<ContentPresenter.ContentTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Symbol, Mode=Default}" />
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</Grid>
</Window>
No comments:
Post a Comment