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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.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
C# 绑定嵌套列表框_C#_Wpf_Mvvm_Binding_Listbox - Fatal编程技术网

C# 绑定嵌套列表框

C# 绑定嵌套列表框,c#,wpf,mvvm,binding,listbox,C#,Wpf,Mvvm,Binding,Listbox,我有一个嵌套在列表框中的列表框 XAML 视图模型 public ObservableCollection<Scene> Scenes { get; set; } public ObservableCollection<Gear> AllEquipment { get; set; } 如何配对EquipmentPersence为特定listboxItem(场景)显示正确的设备。据我所知,您需要一种机制来显示每个场景对象的扩展数据。我可以考虑两种方法 示例 1.第一种是主

我有一个嵌套在列表框中的列表框

XAML

视图模型

public ObservableCollection<Scene> Scenes { get; set; }
public ObservableCollection<Gear> AllEquipment { get; set; }

如何配对EquipmentPersence为特定listboxItem(场景)显示正确的设备。

据我所知,您需要一种机制来显示每个场景对象的扩展数据。我可以考虑两种方法

示例 1.第一种是主/细节方法,每个选定场景都会刷新EquipmentPerScene集合视图。对于这种方法,您需要添加SelectedScene属性,当选定场景发生更改时,您将启动更新方法,该方法将刷新集合视图。此案例将为您提供用于过滤的SceneNumber(SelectedScene对象将)。 2.第二种是场景对象扩展方法,每个场景对象都将像您尝试的那样提供自己的设备集合(您的解决方案具有带有ListBox标记嵌套的包)。对于这种方法,您需要将EquipmentPerScene observable集合属性添加到场景类中,并重新为场景Listbox的每个ListBoxItem设置模板。就像这样:

       <ListBox ItemsSource="{Binding Scenes}">
       <ListBox.ItemContainerStyle>
           <Style TargetType="ListBoxItem">
               <Setter Property="ContentTemplate">
                   <Setter.Value>
                        <DataTemplate DataType="{x:Type soListBoxStyleHelp:Scene}">
                            <StackPanel>
                                <TextBlock>
                                    <Run Text="{Binding Path=SceneNumber}"/>
                                </TextBlock>
                                <ListBox ItemsSource="{Binding EquipmentPerScene}">
                                    <ListBox.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel>
                                                <TextBlock>
                                                    <Run Text="{Binding Path=Item}"/>
                                                </TextBlock>
                                            </StackPanel>
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
               </Setter>
           </Style>
       </ListBox.ItemContainerStyle>
   </ListBox>

如果您在代码和解释方面遇到问题,我很乐意提供帮助。
关于,

为什么不改为使用分组?
public ObservableCollection<Scene> Scenes { get; set; }
public ObservableCollection<Gear> AllEquipment { get; set; }
private ListCollectionView _equipmentPerScene;
public ListCollectionView  EquipmentPerScene
{
    get
    {
        if (_equipmentPerScene == null) //important for loading the app
        {
            _equipmentPerScene = new ListCollectionView(AllEquipment);
            _equipmentPerScene.IsLiveFiltering = true;
            _equipmentPerScene.Filter = o =>
            {
                var gear = o as Gear;
                return gear!= null && gear.SceneNumber == ??????????;
            };
        }
        return _equipmentPerScene;
    }
    set
    {
        _equipmentPerScene = value; RaisePropertyChanged();
    }
 }
       <ListBox ItemsSource="{Binding Scenes}">
       <ListBox.ItemContainerStyle>
           <Style TargetType="ListBoxItem">
               <Setter Property="ContentTemplate">
                   <Setter.Value>
                        <DataTemplate DataType="{x:Type soListBoxStyleHelp:Scene}">
                            <StackPanel>
                                <TextBlock>
                                    <Run Text="{Binding Path=SceneNumber}"/>
                                </TextBlock>
                                <ListBox ItemsSource="{Binding EquipmentPerScene}">
                                    <ListBox.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel>
                                                <TextBlock>
                                                    <Run Text="{Binding Path=Item}"/>
                                                </TextBlock>
                                            </StackPanel>
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
               </Setter>
           </Style>
       </ListBox.ItemContainerStyle>
   </ListBox>