【问题标题】:Binding problem with CurrentItem in WPF C#, Can't access "second level data"WPF C# 中 CurrentItem 的绑定问题,无法访问“二级数据”
【发布时间】:2011-05-05 10:00:59
【问题描述】:

我在 c# wpf 字符串绑定到 CurrentItem 时遇到问题,我有一个人员列表,每个人可以有两个项目之一。您可以在列表中选择一个人,然后在组合框中选择它的项目。

组合框绑定到 Persons.CurrentItem.Item 并显示该人作为选定项拥有的内容。但是我不能改变它,或者更确切地说,我不能保留所做的改变,一旦我选择了一个新的人,它就会变回来。

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>

如您所见,我添加了一个带有 SelectedItem 的 Person2 作为 SelectedPerson。这很好用,我想模仿它的功能,但我想使用当前项目。

这是 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;
        }
    }
}

如果您测试示例,您可以看到 Persons.CurrentItem.Name 有效,但 Persons.CurrentItem.Item.Name 无效,为什么?我错过了访问级别的东西吗? 关于如何使用 CurrentItem,我有什么遗漏的吗?

谢谢你启发我。

【问题讨论】:

    标签: c# wpf .net-4.0 binding currentitem


    【解决方案1】:

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

    解决方案不是使用 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>
    

    【讨论】:

      猜你喜欢
      • 2016-03-16
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 2014-05-25
      • 1970-01-01
      相关资源
      最近更新 更多