gpt4 book ai didi

c# - ListView 绑定(bind)中的 SelectedItem

转载 作者:太空狗 更新时间:2023-10-30 00:33:40 25 4
gpt4 key购买 nike

我是 WPF 新手。在我的示例应用程序中,我使用 ListView 来显示属性的内容。我不知道如何将 ListView 中的 SelectedItem 绑定(bind)到属性,然后再绑定(bind)到 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="First name" DisplayMemberBinding="{Binding FirstName}"/>
<GridViewColumn Header="Last name" DisplayMemberBinding="{Binding LastName}"/>
<GridViewColumn Header="Address" DisplayMemberBinding="{Binding Address}"/>
</GridView>
</ListView.View>
</ListView>

<TextBlock Height="23" Name="textFirstNameBlock" FontSize="12" Margin="97,240,155,144">
<Run Text="Name: " />
<Run Text="{Binding CurrentSelectedPerson.FirstName}" FontWeight="Bold" />
</TextBlock>

<TextBlock Height="23" Name="textLastNameBlock" FontSize="12" Margin="97,263,155,121">
<Run Text="Branch: " />
<Run Text="{Binding CurrentSelectedPerson.LastName}" 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.Address}" 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;
}

}
}

private Person currentSelectedPerson;
public Person CurrentSelectedPerson
{
get
{
return currentSelectedPerson;
}
set
{
this.currentSelectedPerson = value;
}
}


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;
}

}

感谢您的帮助。

最佳答案

private void Window_Loaded(object sender, RoutedEventArgs e){
ListViewPersonDetails.ItemsSource= manager.GetPersons();
}

这可能是您的问题,您不必像这样设置项目源。只需替换为这一行...

this.DataContext = this;

这就是将您的属性绑定(bind)到 UI 的内容,因此所有这些绑定(bind)语句都具有任何意义。

您还需要更新文本 block 上的绑定(bind)以实际匹配 Person 类中的属性名称。

<TextBlock Height="23" Name="textFirstNameBlock" FontSize="12" Margin="97,240,155,144">
<Run Text="Name: " />
<Run Text="{Binding CurrentSelectedPerson.FirstName}" FontWeight="Bold" />
</TextBlock>

<TextBlock Height="23" Name="textLastNameBlock" FontSize="12" Margin="97,263,155,121">
<Run Text="Branch: " />
<Run Text="{Binding CurrentSelectedPerson.LastName}" 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.Address}" FontWeight="Bold" />
</TextBlock>

编辑:

在 VS 中测试,您还需要 1 个小东西,以在 CurrentSelectedPerson 属性上实现 INotifyPropertyChanged...

private Person currentSelectedPerson;
public Person CurrentSelectedPerson
{
get { return currentSelectedPerson; }
set
{
currentSelectedPerson = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("CurrentSelectedPerson"));
}
}

作为替代方案,这也可行,更简单一些......

<TextBlock Height="23" Name="textFirstNameBlock" FontSize="12" Margin="97,240,155,144">
<Run Text="Name: " />
<Run Text="{Binding ElementName=ListViewPersonDetails, Path=SelectedItem.FirstName}" FontWeight="Bold" />
</TextBlock>

(对其他文本框重复类似的逻辑)

关于c# - ListView 绑定(bind)中的 SelectedItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10866423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com