ListBox和DataGrid之间的主 - 细节层次结构

时间:2013-05-11 14:09:10

标签: wpf binding datagrid listbox

我想根据ListBox.SelectedItem值设置DataGrid.ItemsSource属性,这样无论何时在ListBox中选择一个类别,DataGrid都应显示相应的集合。

示例:

ListBox包含以下项目:

  • 艺术家
  • 媒体

(摘自此枚举)

public enum Category
{
    Artists,
    Medias
}

然后DataGrid应该从这里选择相关的集合:

public class MyCategories
{
    public IEnumerable<Media> Medias { get; set; }
    public IEnumerable<Artist> Artists { get; set; }
}

但是,DataGrid.ItemsSource的ConverterParameter不允许为它定义绑定。

无法在“Binding”类型的“ConverterParameter”属性上设置“绑定”。 '绑定'只能在DependencyObject的DependencyProperty上设置。

    <DataGrid ItemsSource="{Binding Mode=OneWay,
              Converter={StaticResource categoriesConverter},
              ConverterParameter={Binding ElementName=listBox1, Path=SelectedItem}}" />

如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

一种方法是创建一个包含name和IEnumerable的类 在你的情况下,IEnumerable是一个类(不是字符串),在路径中你需要包含要显示的属性的名称

<Window x:Class="Waster13.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ListBox Grid.Column="0" ItemsSource="{Binding Path=EnumsPlus}" DisplayMemberPath="Name" x:Name="lbMaster"/>
        <ListBox Grid.Column="1" ItemsSource="{Binding ElementName=lbMaster, Path=SelectedItem.IEnum}" />
    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;

namespace Waster13
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<EnumPlus> enumsPlus = new List<EnumPlus>();
        public MainWindow()
        {
            this.DataContext = this;
            enumsPlus.Add(new Media("media1", new List<string>() { "m1", "m2" }));
            enumsPlus.Add(new Media("media2", new List<string>() { "m3", "m4" }));
            enumsPlus.Add(new Artist("artist1", new List<string>() { "a1", "a2" }));
            enumsPlus.Add(new Artist("artist2", new List<string>() { "a3", "a4" }));
            InitializeComponent();
        }
        public List<EnumPlus> EnumsPlus { get { return enumsPlus; } }
    }
    public interface EnumPlus : IEnumerable
    {
        String Name { get; }
    }
    public class Media : EnumPlus
    {
        private string name;
        private IEnumerable iEnum;
        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator)iEnum.GetEnumerator();
        }
        public String Name { get { return name; } }
        public IEnumerable IEnum { get { return iEnum; } }

        public Media(string Name, IEnumerable IEnum)
        { name = Name; iEnum = IEnum; }
    }
    public class Artist : EnumPlus
    {
        private string name;
        private IEnumerable iEnum;
        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator)iEnum.GetEnumerator();
        }
        public String Name { get { return name; } }
        public IEnumerable IEnum { get { return iEnum; } }

        public Artist(string Name, IEnumerable IEnum)
        { name = Name; iEnum = IEnum; }
    }
}