Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF ListBox选定项,在失去焦点时保持选定状态_Wpf_Xaml_Mvvm_Listbox_Inotifypropertychanged - Fatal编程技术网

WPF ListBox选定项,在失去焦点时保持选定状态

WPF ListBox选定项,在失去焦点时保持选定状态,wpf,xaml,mvvm,listbox,inotifypropertychanged,Wpf,Xaml,Mvvm,Listbox,Inotifypropertychanged,我有一个父列表框,它由两个列表框和一个子列表框组成,所以总共有3个列表框。前两个的项目源只是两个独立的集合,这些集合包含内部集合,然后成为第三个列表框的项目源。如果选择第二项,则再次填充子列表框;如果转到第二个父列表框并选择某项,则填充子列表框。问题是,如果此时我返回到第一个列表框,并尝试选择以前选择的项目,则什么也不会发生,而我希望它再次填充子列表框 希望这是有意义的,这里是XAML: <Window x:Class="ExampleForStackOverFlow.MainWi

我有一个父列表框,它由两个列表框和一个子列表框组成,所以总共有3个列表框。前两个的项目源只是两个独立的集合,这些集合包含内部集合,然后成为第三个列表框的项目源。如果选择第二项,则再次填充子列表框;如果转到第二个父列表框并选择某项,则填充子列表框。问题是,如果此时我返回到第一个列表框,并尝试选择以前选择的项目,则什么也不会发生,而我希望它再次填充子列表框

希望这是有意义的,这里是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");
        }
    }
}
使用System.Collections.ObjectModel;
命名空间ExampleForStackOverFlow
{
公共类MainWindowViewModel:NotifyPropertyChanged
{
公共主窗口视图模型()
{
Parents1=新的ObservableCollection();
Parents2=新的ObservableCollection();
foreach(ParentCollection.GetParents()中的var parent)
{
如果(parent.Name==“parent 1”| | parent.Name==“parent 2”)
{
家长1.添加(家长);
}
其他的
{
Parents2.添加(parent);
}
}
Children=新的ObservableCollection();
}
公共ObservableCollection父项1{get;set;}
公共observeCollection Parents2{get;set;}
私生子;
公众观察收集儿童
{
获取{返回子项;}
设置
{
儿童=价值;
财产变更(“子女”);
}
}
私人家长选择家长;
公共父级选择父级
{
获取{return selectedParent;}
设置
{
selectedParent=值;
Children=所选父项。Children;
OnPropertyChanged(“SelectedParent”);
}
}
私人家长选择家长2;
公共父选择父2
{
获取{return selectedParent2;}
设置
{
selectedParent2=值;
Children=所选家长2.子女;
OnPropertyChanged(“SelectedParent2”);
}
}
}
公共类父级:NotifyPropertyChanged
{
私有字符串名称;
公共字符串名
{
获取{返回名称;}
设置
{
名称=值;
不动产变更(“名称”);
}
}
私人可观察收集儿童;
公众观察收集儿童
{
获取{return childres;}
设置
{
childres=价值;
财产变更(“子女”);
}
}
}
公共类子级:NotifyPropertyChanged
{
私有字符串名称;
公共字符串名
{
获取{返回名称;}
设置
{
名称=值;
不动产变更(“名称”);
}
}
}

}

所选的值没有更改,因此它不会触发集合
如果将其设置为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");
        }
    }

非常感谢你,我需要保留额外的财产更改。我花了一整天的时间试图弄清楚如何取消选择某个项目等。再次感谢:)