【问题标题】:WPF - Nested Listbox Binding Not WorkingWPF - 嵌套列表框绑定不起作用
【发布时间】:2018-06-16 15:36:13
【问题描述】:

我正在处理一个嵌套了listbox 的项目。 inner-listbox 的ItemsSource 引用了在其父列表框的ItemsSource 中定义的collection of string。但是当我运行代码时,我无法在嵌套列表框中看到绑定的数据

我搜索了论坛并找到了thisthis 解决方案,但我还没有解决问题。也许代码有问题。下面是代码的样子:

<ListBox ItemsSource="{Binding Items}" Name="detailList" Margin="5,5,0,0">

    <ListBox.ItemTemplate >
        <DataTemplate>
            <StackPanel Orientation="Vertical" Width="90" Margin="5,0,0,0">
                <TextBlock Text="{Binding Name}" Width="60" Height="30" TextWrapping="Wrap" FontSize="11" TextAlignment="Center"/>
                <Expander Visibility="{Binding Visibility}" DockPanel.Dock="Top">

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

                </Expander>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

代码背后

public ObservableCollection<DirectoryItemViewModel> Items { get; set; }
public DefaultControl()
{
    InitializeComponent();
    Items = new ObservableCollection<DirectoryItemViewModel>
    {
        /*Some Initial Data*/
    };
    this.DataContext = Items;
}

DirectoryItemViewModel.cs

public class DirectoryItemViewModel : INotifyPropertyChanged 
{

    public event PropertyChangedEventHandler PropertyChanged;       
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    // Constructor
    public DirectoryItemViewModel()
    {            
        //initialize some data          

        _subfamiliesnames = new ObservableCollection<string>();
        _subfamiliesnames.Add("File1");
        _subfamiliesnames.Add("File2");
        _subfamiliesnames.Add("File3");

    }

    #region Public Properties

    public ObservableCollection<string> _subfamiliesnames;        
    public ObservableCollection<string> SubFamiliesNames
    {
        get { return _subfamiliesnames; }
        set
        {
            _subfamiliesnames = value;
            OnPropertyChanged("SubFamiliesNames");
        }
    }

    // Full path of the directory
    public string FullPath { get; set; }

    // Item type (Enum)
    public DirectoryItemType Type { get; set; }  

    public string Visibility
    {
        get
        { 
           return "Visible";               
        }
    }

    //The name of the Directory
    public string Name { get { return this.Type == DirectoryItemType.Drive ? this.FullPath : DirectoryStructure.GetFileFolderName(FullPath); } }

    #endregion

}

请分享您的解决方案,非常感谢您的支持。谢谢。

【问题讨论】:

  • 以这种方式嵌套列表框并没有多大意义——我会再看看你想要实现的 UI 的想法表示是什么?通常,树更适合分层数据。
  • @AdamBrown 根据我的用例,我不能使用树来表示数据。因此,嵌套列表框是一种前进的方式。但我在数据绑定方面苦苦挣扎。拜托,你能看看这个吗?

标签: c# wpf xaml data-binding listbox


【解决方案1】:

在后面的代码中,数据上下文被指定为 Items。 所以对于detailList的ItemSource,它会尝试在Items集合中找到Items的出现。

尝试在后面的代码中指定 this.DataContext = this;。 然后它将尝试在 DefaultControl 类中查找“Items”,并成功找到它。

如果它仍然没有在您的列表视图中显示所需的数据,请告诉我。

After modified code, the listbox shows data like following

【讨论】:

  • DataContext = this;在控件中被认为是非常糟糕的做法,因为它会破坏父级的所有绑定。
  • 我试过了,但也没有用...您能建议任何其他解决方法吗?
  • 一切都是为了找到您正在绑定的项目。如果您的 ItemSource 存在于代码隐藏中,则需要执行此操作。为了更好的实践,您可以创建另一个视图模型并将项目移动到该视图模型并将数据上下文设置为新创建的视图模型。答案基于发布者的代码。
  • 很有趣,您的解释已经有所启发。将再次尝试解决方案。谢谢
  • 我检查了截图,只有一个问题,你能把xaml分享给我吗?我想知道您是如何处理嵌套列表框绑定的。谢谢
猜你喜欢
  • 1970-01-01
  • 2011-10-15
  • 1970-01-01
  • 2010-11-29
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多