WPF ListBox选中的项目,在失去焦点时保持选中状态

时间:2014-03-20 15:53:47

标签: wpf xaml mvvm listbox inotifypropertychanged

我有一个父ListBox,它由两个列表框和一个子列表框组成,总共有3个列表框。前两个的Item源只是两个独立的集合,这些集合包含内部集合,然后成为第三个列表框的项目源。如果我选择第二项,则再次填充子列表框,如果我转到第二个父列表框并选择了某些内容,则会填充子列表框。问题是,如果此时我回到第一个列表框,并尝试选择之前选择的项目,没有任何反应,而我期望它再次填充子列表框。

希望这是有道理的,这是XAML:

    <Window x:Class="ExampleForStackOverFlow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="350" Height="350">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <StackPanel>
        <TextBlock Text="Parents" FontSize="20" FontWeight="Bold" Background="LightBlue"/>
        <TextBlock Text="First Listbox" FontSize="16" FontWeight="Bold" Background="LightBlue"/>
        <ListBox ItemsSource="{Binding Parents1}" SelectedItem="{Binding SelectedParent}" BorderThickness="0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <TextBlock Text="Second Listbox" FontSize="16" FontWeight="Bold" Background="LightBlue"/>
        <ListBox ItemsSource="{Binding Parents2}" SelectedItem="{Binding SelectedParent2}" BorderThickness="0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
    <StackPanel Grid.Column="1">
        <TextBlock Text="Children" FontSize="16" FontWeight="Bold" Background="LightBlue"/>
        <ListBox ItemsSource="{Binding Children}" BorderThickness="0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Grid>

这是ViewModel

    using System.Collections.ObjectModel;

    namespace ExampleForStackOverFlow
    {
  public class MainWindowViewModel : NotifyPropertyChanged
   {
    public MainWindowViewModel()
    {
        Parents1 = new ObservableCollection<Parent>();
        Parents2 = new ObservableCollection<Parent>();
        foreach (var parent in ParentCollection.GetParents())
        {
            if (parent.Name == "Parent 1" || parent.Name == "Parent 2")
            {
                Parents1.Add(parent);
            }
            else
            {
                Parents2.Add(parent);
            }
        }
        Children = new ObservableCollection<Child>();
    }

    public ObservableCollection<Parent> Parents1 { get; set; }
    public ObservableCollection<Parent> Parents2 { get; set; }

    private ObservableCollection<Child> children;
    public ObservableCollection<Child> Children 
    {
        get { return children; }
        set
        {
            children = value;
            OnPropertyChanged("Children");
        }
    }

    private Parent selectedParent;
    public Parent SelectedParent
    {
        get { return selectedParent; }
        set 
        {
            selectedParent = value;
            Children = SelectedParent.Children;
            OnPropertyChanged("SelectedParent");
        }
    }

    private Parent selectedParent2;
    public Parent SelectedParent2
    {
        get { return selectedParent2; }
        set
        {
            selectedParent2 = value;
            Children = SelectedParent2.Children;
            OnPropertyChanged("SelectedParent2");
        }
    }
}

public class Parent : NotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get { return name; }
        set 
        {
            name = value;
            OnPropertyChanged("Name");
        }
    }

    private ObservableCollection<Child> childres;
    public ObservableCollection<Child> Children
    {
        get { return childres; }
        set 
        {
            childres = value;
            OnPropertyChanged("Children");
        }
    }
}

public class Child : NotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get { return name; }
        set 
        {
            name = value;
            OnPropertyChanged("Name");
        }
    }
}

}

1 个答案:

答案 0 :(得分:1)

所选的值没有改变,因此它不会触发设置
如果将其设置为null,则重新选择将是新值

    private Parent selectedParent;
    public Parent SelectedParent
    {
        get { return selectedParent; }
        set 
        {
            selectedParent2 = null;
            selectedParent = value;
            Children = SelectedParent.Children;
            OnPropertyChanged("SelectedParent");
            OnPropertyChanged("SelectedParent2");  // you may not need this
        }
    }

    private Parent selectedParent2;
    public Parent SelectedParent2
    {
        get { return selectedParent2; }
        set
        {
            selectedParent = null;
            selectedParent2 = value;
            Children = SelectedParent2.Children;
            OnPropertyChanged("SelectedParent");  // you may not need this
            OnPropertyChanged("SelectedParent2");
        }
    }