C# 在ListView绑定中选择EdItem

C# 在ListView绑定中选择EdItem,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我是WPF的新手。在我的示例应用程序中,我使用ListView来显示属性的内容。我不知道如何将ListView中的SelectedItem绑定到属性,然后绑定到TextBlock Window.xaml <Window x:Class="Exec.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.co

我是WPF的新手。在我的示例应用程序中,我使用ListView来显示属性的内容。我不知道如何将ListView中的SelectedItem绑定到属性,然后绑定到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>
谢谢你的帮助

private void Window_Loaded(object sender, RoutedEventArgs e){
    ListViewPersonDetails.ItemsSource= manager.GetPersons();
}
这可能是您的问题,您不应该像这样设置itemssource。只要换成这一行

this.DataContext = this;
这就是将您的属性绑定到UI的方法,因此所有这些绑定语句都有任何意义

此外,还需要更新textblocks上的绑定,以实际匹配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>
作为另一种选择,这也可以,简单一点

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


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

NotifyPropertyChanged需要发送属性名称,您出于某种原因尝试发送不同的字符串?不确定这是否是唯一的问题,列表是否填充了所有内容?您现在得到了什么行为?您正在设置窗口的DataContext吗?需要设置这些绑定才能工作。另外,您将两次设置ItemsSource,一次在XAML中,一次在代码中。您应该只需要设置一次。@KDiTraglia抱歉,我忘了删除NotifyPropertyChanged。我知道它可以工作,但我不知道如何将CurrentSelectedPerson属性项绑定到TextBlocks..谢谢,这很有帮助。在CurrentSelectedPerson中,属性为SelectedItem。您知道如何将CurrentSelectedPerson项目绑定到文本块吗?它应该按照您列出的方式工作,只需更正名称以实际匹配属性(CurrentSelectedPerson.FirstName而不是FstNamePerson)。将编辑文章。我不知道为什么,但它不工作。我编辑了我的第一篇文章。无论如何,谢谢你的帮助。我必须将Mode=“TwoWay”添加到SelectedItem=“{Binding CurrentSelectedPerson}”才能让他工作
<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>
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>