简体   繁体   English

WPF C# 中的 CurrentItem 绑定问题,无法访问“二级数据”

[英]Binding problem with CurrentItem in WPF C#, Can't access “second level data”

I have a problem in c# wpf tring to bind to CurrentItem, i have a list of persons, and each persons can have one of two items.我在 c# wpf 绑定到 CurrentItem 时遇到问题,我有一个人员列表,每个人可以拥有两个项目之一。 You can select a person in the list and then select it's item in a combobox.您可以 select 列表中的一个人,然后 select 它是 combobox 中的项目。

The combobox binds to Persons.CurrentItem.Item and shows what the person have as selected item. combobox 绑定到 Persons.CurrentItem.Item 并显示该人作为所选项目拥有的内容。 But i cant change it, or rather i cant keep the change that is made, it changes back as soon as i select a new person.但我不能改变它,或者更确切地说,我不能保留所做的改变,只要我 select 换了一个新人,它就会变回来。

The XAML looks like this: XAML 看起来像这样:

    <!--This dose not work-->
    <TextBox Height="23" HorizontalAlignment="Left" Margin="148,55,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding Path=Persons.CurrentItem.Item.Name}"/>

    <!--This works-->
    <TextBox Height="23" HorizontalAlignment="Left" Margin="16,306,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding Path=Persons.CurrentItem.Name}"/>

    <!--This works, we do not bind to CurrentItem-->
    <ListBox Height="274" HorizontalAlignment="Left" ItemsSource="{Binding Path=Persons2}" SelectedItem="{Binding Path=SelectedPerson}" Margin="292,26,0,0" Name="listBox2" VerticalAlignment="Top" Width="120" DisplayMemberPath="Name" />
    <ComboBox Height="23" HorizontalAlignment="Left" ItemsSource="{Binding Path=Items}" SelectedItem="{Binding Path=SelectedPerson.Item}" Margin="431,26,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" DisplayMemberPath="Name"/>
</Grid>

As you can see i have added a persons2 with SelectedItem as SelectedPerson.如您所见,我添加了一个带有 SelectedItem 的 Person2 作为 SelectedPerson。 This works fine and i want to mimic it's function but i want to use Current item.这很好用,我想模仿它的 function 但我想使用当前项目。

This is the C# code:这是 C# 代码:

    // Selectable items
    public List<Item> Items { get; set; }

    // List of persons, we will bind to it's CurrentItem
    public List<Person> Persons { get; set; }

    // This works, we do not use CurrentItem
    public List<Person> Persons2 { get; set; }
    private Person _selectedPerson;
    public Person SelectedPerson
    {
        get { return _selectedPerson; }
        set
        {
            _selectedPerson = value;
            NotifyPropertyChanged("SelectedPerson");
        }
    }


    #region Constructo
    public Window()
    {
        InitializeComponent();
        DataContext = this;

        // Populate Items
        Items = new List<Item>
                    {
                        new Item {Name = "Hammer"},
                        new Item {Name = "Axe"}
                    };

        // Populate persons
        Persons = new List<Person>() { new Person { Name = "Lisa", Item = Items.FirstOrDefault()}, new Person { Name = "Kauf" } };
        Persons2 = new List<Person>(Persons); // make a copy
    }
    #endregion

    #region PropertyChangeHandler
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string name)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(name));
    }
    #endregion
}

public class Item
{
    public string Name { get; set; }
}

public class Person
{
    public string Name { get; set; }
    private Item _item;
    public Item Item
    {
        get { return _item; }
        set
        {
            // We only accass this if we do not bind to CurrentItem
            _item = value;
        }
    }
}

If you test the example you can see that Persons.CurrentItem.Name works, but Persons.CurrentItem.Item.Name dose not, Why?如果您测试示例,您可以看到 Persons.CurrentItem.Name 有效,但 Persons.CurrentItem.Item.Name 无效,为什么? Have i missed something with the level of access?我错过了访问级别的东西吗? Is there something i have missed on how to use CurrentItem?关于如何使用 CurrentItem,我有什么遗漏的吗?

Thanks for enlightening me.谢谢你启发我。

Asked the same question on msdn: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/18eee956-3a91-4db3-a4ff-7e8d127ae301在 msdn 上问了同样的问题: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/18eee956-3a91-4db3-a4ff-7e8d127ae301

The solution is not use CurentItem but instead / like this:解决方案不是使用 CurentItem 而是/像这样:

<StackPanel>
      <ListBox ItemsSource="{Binding Path=Persons}" IsSynchronizedWithCurrentItem="True" DisplayMemberPath="Name" />
      <ComboBox HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding Path=Items}" SelectedItem="{Binding Persons/Item}" DisplayMemberPath="Name"/>
</StackPanel>

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

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