WPF - 如何从两个链接 collection 进行绑定?

WPF - How to binding from two linked collection?

第一个 class 包含 idGendre

public class  Book: INotifyPropertyChanged
{
    private string name;
    private int idGendre;
}

第二个 class 包含对

的描述
public class  Gendre: INotifyPropertyChanged
{
    private int idGendre;
    private string gendre;
}

Class 与 collections

public class Data
    {
        private ObservableCollection<Book> _booksCollection;
        private ObservableCollection<Gendre> _gendresCollection;
    }

XAML:

<DataGrid ItemsSource="{Binding BooksCollection}" AutoGenerateColumns="False">
          <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Gendre id" Binding="{Binding IdGendre}"/>   
       <DataGridTextColumn Header="Gendre name" Binding="{Binding IdGendre, Converter={StaticResource convertor}}"/>
            <DataGridTextColumn Header="Gendre Name" Binding="{?????????????}"/>  
          </DataGrid.Columns>
        </DataGrid>

WPF 代码隐藏

public partial class MainWindow : Window
    {
        Data data;      
        public MainWindow()
        {
            InitializeComponent();
            data= new data();
            DataContext = data;
        }
    }

我尝试 IValueConverter 但我可以像那样使用它

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                switch (value.ToString().ToLower())
                {
                    case "1":
                        return "scify";
                    case "2":
                        return "comics";
                         }
                return "";
            }

有没有办法用主密钥从另一个 collection 绑定? 谢谢

您需要通过加入 Books and Gendre 集合来创建一个新集合,select 所需的属性并将此集合绑定到 DataGrid。 This 可能对你有帮助。