SelectedItem in ListView binding
- by Matt
I'm new in wfp. In my sample application I'm using a ListView to display contents of property. I don't know how to bind SelectedItem in ListView to property and then bind to TextBlock.
Window.xaml
<Window x:Class="Exec.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main window" Height="446" Width="475" >
<Grid>
<ListView Name="ListViewPersonDetails" Margin="15,12,29,196" ItemsSource="{Binding Persons}" SelectedItem="{Binding CurrentSelectedPerson}">
<ListView.View>
<GridView>
<GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FstNamePerson}"/>
<GridViewColumn Header="LastName" DisplayMemberBinding="{Binding SndNamePerson}"/>
<GridViewColumn Header="Address" DisplayMemberBinding="{Binding AdressPerson}"/>
</GridView>
</ListView.View>
</ListView>
<TextBlock Height="23" Name="textFirstNameBlock" FontSize="12" Margin="97,240,155,144">
<Run Text="Name: " />
<Run Text="{Binding CurrentSelectedPerson.FstNamePerson}" FontWeight="Bold" />
</TextBlock>
<TextBlock Height="23" Name="textLastNameBlock" FontSize="12" Margin="97,263,155,121">
<Run Text="Branch: " />
<Run Text="{Binding CurrentSelectedPerson.SndNamePerson}" FontWeight="Bold" />
</TextBlock>
<TextBlock Height="23" Name="textAddressBlock" FontSize="12" Margin="0,281,155,103" HorizontalAlignment="Right" Width="138">
<Run Text="City: " />
<Run Text="{Binding CurrentSelectedPerson.AdressPerson}" FontWeight="Bold" />
</TextBlock>
</Grid>
</Window>
MainWindow.xaml.cs
Tman manager = new Tman();
private List<Person> persons;
public List<Person> Persons
{
get
{
return this.persons;
}
set
{
if (value != null)
{
this.persons = value;
this.NotifyPropertyChanged("Data");
}
}
}
private Person currentSelectedPerson;
public Person CurrentSelectedPerson
{
get
{
return currentSelectedPerson;
}
set
{
this.currentSelectedPerson = value;
this.NotifyPropertyChanged("CurrentSelectedItem");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private void Window_Loaded(object sender, RoutedEventArgs e){
ListViewPersonDetails.ItemsSource= manager.GetPersons();
}
Person.cs
class Person
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public string Address
{
get;
set;
}
}
Thanks for any help.