在的SelectedItem绑定的ListView [英] SelectedItem in ListView binding

查看:149
本文介绍了在的SelectedItem绑定的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我新的WPF。在我的示例应用程序我用一个ListView显示的财产内容。我不知道如何在ListView控件绑定的SelectedItem财产,然后绑定到TextBlock的。



Window.xaml



 < ;窗口x:类=Exec.App
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =HTTP://模式.microsoft.com / WinFX的/ 2006 / XAML
标题=主窗口HEIGHT =446WIDTH =475>

<网格和GT;
< ListView控件名称=ListViewPersonDetails保证金=15,12,29,196的ItemsSource ={结合人}的SelectedItem ={结合CurrentSelectedPerson}>
< ListView.View>
<&GridView的GT;
< GridViewColumn标题=名字DisplayMemberBinding ={结合姓}/>
< GridViewColumn标题=姓DisplayMemberBinding ={结合姓氏}/>
< GridViewColumn标题=地址DisplayMemberBinding ={绑定地址}/>
< / GridView的>
< /ListView.View>
< /&的ListView GT;

< TextBlock的高度=23NAME =textFirstNameBlock字号=12保证金=97240155144>
<运行文本=名称:/>
<运行文本={结合CurrentSelectedPerson.FirstName}粗细=大胆/>
< / TextBlock的>

< TextBlock的高度=23NAME =textLastNameBlock字号=12保证金=97263155121>
<运行文本=科:/>
<运行文本={结合CurrentSelectedPerson.LastName}粗细=大胆/>
< / TextBlock的>

< TextBlock的高度=23NAME =textAddressBlock字号=12保证金=0281155103的Horizo​​ntalAlignment =右WIDTH =138>
<运行文本=城市:/>
<运行文本={结合CurrentSelectedPerson.Address}粗细=大胆/>
< / TextBlock的>

< /网格和GT;
< /窗GT;



MainWindow.xaml.cs



  TMAN经理=新TMAN(); 

私人列表<&人GT;者;
公开名单<&人GT;人
{
得到
{
返回this.persons;
}


{
如果
{
this.persons =价值(价值!= NULL);
}

}
}

私募人士currentSelectedPerson;
公众人物CurrentSelectedPerson
{
得到
{
返回currentSelectedPerson;
}

{
this.currentSelectedPerson =价值;
}
}


私人无效Window_Loaded(对象发件人,RoutedEventArgs E){
ListViewPersonDetails.ItemsSource = manager.GetPersons();

}



Person.cs

 类Person 
{
公共字符串名字
{
搞定;
组;
}

公共字符串姓氏
{
获得;
组;
}

公共字符串地址
{
获得;
组;
}

}



感谢您的帮助。


解决方案

 私人无效Window_Loaded(对象发件人,RoutedEventArgs E){
ListViewPersonDetails.ItemsSource = manager.GetPersons();
}

这可能是你的问题,你不应该设置喜欢的ItemsSource这个。只是这条线替换...

  this.DataContext =这一点; 

这是什么将绑定你的属性的UI,使所有这些绑定声明有任何意义。



另外你需要更新你的TextBlocks实际匹配Person类的属性名称的绑定。

 < TextBlock的高度=23NAME =textFirstNameBlock字号=12保证金=97240155144> 
<运行文本=名称:/>
<运行文本={结合CurrentSelectedPerson.FirstName}粗细=大胆/>
< / TextBlock的>

< TextBlock的高度=23NAME =textLastNameBlock字号=12保证金=97263155121>
<运行文本=科:/>
<运行文本={结合CurrentSelectedPerson.LastName}粗细=大胆/>
< / TextBlock的>

< TextBlock的高度=23NAME =textAddressBlock字号=12保证金=0281155103的Horizo​​ntalAlignment =右WIDTH =138>
<运行文本=城市:/>
<运行文本={结合CurrentSelectedPerson.Address}粗细=大胆/>
< / TextBlock的>



编辑:



经测试,在VS,你需要1个很小的事情,要在currentSelectedPerson属性实现INotifyPropertyChanged ...

 私募人士currentSelectedPerson; 
公众人物CurrentSelectedPerson
{
{返回currentSelectedPerson; }

{
currentSelectedPerson =价值;
如果(的PropertyChanged!= NULL)的PropertyChanged(这一点,新PropertyChangedEventArgs(CurrentSelectedPerson));
}
}



作为替代这个工程太简单了一点。 ..

 < TextBlock的高度=23NAME =textFirstNameBlock字号=12保证金=97240155144> ; 
<运行文本=名称:/>
<运行文本={绑定的ElementName = ListViewPersonDetails,路径= SelectedItem.FirstName}粗细=大胆/>
< / TextBlock的>



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


I'm new to WPF. 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="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;
        }

    }

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

Also you need to update the binding on your textblocks to actually match the names of the properties in the Person class.

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

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)

这篇关于在的SelectedItem绑定的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆