【问题标题】:Binding nested Listbox绑定嵌套列表框
【发布时间】:2015-12-06 09:36:49
【问题描述】:

我有一个嵌套在 ListBox 中的 ListBox。

XAML

<ListBox ItemsSource="{Binding Scenes}">

<TextBox Text="{Binding Path=SceneNumber}"/>

   <ListBox ItemsSource="{Binding EquipmentPerScene}"> 
     <ListBox.ItemTemplate>
          <DataTemplate>
             <StackPanel>
                 <TextBlock>
                       <Run Text="{Binding Path=Item}"/>
                 </TextBlock>
             </StackPanel>
         </DataTemplate>
      </ListBox.ItemTemplate>
   </ListBox>
</ListBox>

我有两个系列。一个“场景”包含所有场景。我还有另一个收藏,其中包含设备。现在我想将设备集合与场景配对,以便显示每个场景的设备。我该怎么做?

型号

public class Scene 
{
    public Scene(string SceneNumber, string SlugLine)
}

public class Gear 
{
    public Equipment(string SceneNumber, string Item)
}

视图模型

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();
    }
 }

我怎样才能让 EquipmentPerScene 显示该特定 listboxItem(场景)的正确设备。

【问题讨论】:

  • 为什么不使用分组来代替?

标签: c# wpf mvvm binding listbox


【解决方案1】:

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

示例 1.第一种是master/details方法,每个选定的场景都会刷新 EquipmentPerScene 集合视图。对于这种方法,您需要添加 SelectedScene 属性,当所选场景发生更改时,您将启动 Update 方法,该方法将刷新您的集合视图。此案例将为您提供用于过滤的 SceneNumber(SelectedScene 对象将)。 2. 第二种是Scene 对象扩展方法,每个Scene 对象都将提供自己的设备集合,就像您尝试做的那样(您的解决方案有带有ListBox 标签嵌套的包)。对于这种方法,您需要将 EquipmentPerScene 可观察集合属性添加到 Scene 类并重新模板化 Scenes 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>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 2016-09-27
    • 2019-08-07
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    相关资源
    最近更新 更多