简体   繁体   English

ListView绑定中的SelectedItem

[英]SelectedItem in ListView binding

I'm new to WPF. 我是WPF的新手。 In my sample application I'm using a ListView to display contents of property. 在我的示例应用程序中,我使用ListView来显示属性的内容。 I don't know how to bind SelectedItem in ListView to property and then bind to TextBlock. 我不知道如何将ListView中的SelectedItem绑定到属性,然后绑定到TextBlock。

Window.xaml 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 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 Person.cs

class Person
    {
        public string FirstName
        {
            get;
            set;
        }

        public string LastName
        {
            get;
            set;
        }

        public string Address
        {
            get;
            set;
        }

    }

Thanks for any help. 谢谢你的帮助。

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

This is probably your problem, you shouldn't have to set the itemssource like this. 这可能是你的问题,你不应该像这样设置itemssource。 Just replace with this line... 只需更换这条线......

this.DataContext = this;

This is what will bind your properties to the UI, so that all those binding statements have any meaning. 这是将属性绑定到UI的内容,因此所有这些绑定语句都有任何意义。

Also you need to update the binding on your textblocks to actually match the names of the properties in the Person class. 您还需要更新文本块上的绑定,以实际匹配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>

EDIT: 编辑:

Tested in VS, you need 1 more small thing, to implement INotifyPropertyChanged on the CurrentSelectedPerson property... 在VS中测试,你还需要1个小东西,在CurrentSelectedPerson属性上实现INotifyPropertyChanged ......

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

As an alternative this works too, a little simpler... 作为替代方案,这也有效,更简单......

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

(repeat similar logic for other text boxes) (对其他文本框重复类似的逻辑)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM