簡體   English   中英

ListView綁定中的SelectedItem

[英]SelectedItem in ListView 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.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();
}

這可能是你的問題,你不應該像這樣設置itemssource。 只需更換這條線......

this.DataContext = this;

這是將屬性綁定到UI的內容,因此所有這些綁定語句都有任何意義。

您還需要更新文本塊上的綁定,以實際匹配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>

(對其他文本框重復類似的邏輯)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM