0

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. You can select a person in the list and then select it's item in a combobox.

The combobox binds to Persons.CurrentItem.Item and shows what the person have as selected 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.

The XAML looks like this:

    <!--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. This works fine and i want to mimic it's function but i want to use Current item.

This is the C# code:

    // 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? Have i missed something with the level of access? Is there something i have missed on how to use CurrentItem?

Thanks for enlightening me.

4

1 回答 1

0

Asked the same question on 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:

<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>
于 2011-05-09T11:35:23.680 回答