WPF DataGrid绑定到列表和子列表



我有一个具有IdNameList<int>属性的Bin对象列表,我将其加载为ComboBoxItemsSource,以及具有IdName属性的List<Part>对象的DataGrid

现在我可以通过组合框选择单个元素。我想更新DataGrid以仅显示第一个Bin对象列表中的元素,并且我想显示Id,而不是Name

不幸的是,我没有发现任何关于这个问题的信息。也许我忽略了什么。这是我的代码:

主窗口.xaml

<Window x:Class="DataGridBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataGridBinding"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400">
<Grid>
<StackPanel Orientation="Vertical">
<ComboBox Name="CmbList" Height="23" Width="180" Margin="6">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<DataGrid x:Name="DtgItems" Width="350" Margin="6" 
ItemsSource="{Binding}" 
SelectedValue="{Binding ElementName=CmbList, Path=SelectedItem.ItemsNr}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding}"/>
<DataGridTextColumn Binding="{Binding Id}"/>
<DataGridTextColumn Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Grid>
</Window>

主窗口.xaml.cs

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
namespace DataGridBinding
{
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
this.CmbList.ItemsSource = new List<Bin>
{
new Bin
{
Id = 1,
Name = "Object One",
ItemsNr = new List<int> { 1, 3, 5, }
},
new Bin
{
Id = 2,
Name = "Object Two",
ItemsNr = new List<int> { 2, 4, 6, }
},
new Bin
{
Id = 3,
Name = "Object Three",
ItemsNr = new List<int> { 7, 8, 9, }
},
new Bin
{
Id = 4,
Name = "Object Four",
ItemsNr = new List<int> { 4, 8 }
},
};
this.DtgItems.DataContext = new List<Part>
{
new Part { Id = 1, Name = "One" },
new Part { Id = 2, Name = "Two" },
new Part { Id = 3, Name = "Three" },
new Part { Id = 4, Name = "Four" },
new Part { Id = 5, Name = "Five" },
new Part { Id = 6, Name = "Six" },
new Part { Id = 7, Name = "Seven" },
new Part { Id = 8, Name = "Eight" },
new Part { Id = 9, Name = "Nine" },
};
this.CmbList.SelectedIndex = 0;
}
}
public class Bin
{
public int Id { get; set; }
public string Name { get; set; }
public List<int> ItemsNr { get; set; }
}
public class Part
{
public int Id { get; set; }
public string Name { get; set; }
}
}

下面是一个工作示例:

<Window x:Class="DataGridBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataGridBinding"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400">
<Grid>
<StackPanel Orientation="Vertical">
<ComboBox Name="CmbList" Height="23" Width="180" Margin="6" SelectionChanged="CmbList_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<DataGrid x:Name="DtgItems" Width="350" Margin="6"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
namespace DataGridBinding
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CmbList.ItemsSource = Bins;
DtgItems.ItemsSource = Parts;
}
List<Bin> Bins = new List<Bin>
{
new Bin
{
Id = 1,
Name = "Object One",
ItemsNr = new List<int> { 1, 3, 5, }
},
new Bin
{
Id = 2,
Name = "Object Two",
ItemsNr = new List<int> { 2, 4, 6, }
},
new Bin
{
Id = 3,
Name = "Object Three",
ItemsNr = new List<int> { 7, 8, 9, }
},
new Bin
{
Id = 4,
Name = "Object Four",
ItemsNr = new List<int> { 4, 8 }
},
};
List<Part> Parts = new List<Part>
{
new Part { Id = 1, Name = "One" },
new Part { Id = 2, Name = "Two" },
new Part { Id = 3, Name = "Three" },
new Part { Id = 4, Name = "Four" },
new Part { Id = 5, Name = "Five" },
new Part { Id = 6, Name = "Six" },
new Part { Id = 7, Name = "Seven" },
new Part { Id = 8, Name = "Eight" },
new Part { Id = 9, Name = "Nine" },
};
public class Bin
{
public int Id { get; set; }
public string Name { get; set; }
public List<int> ItemsNr { get; set; }
}
public class Part
{
public int Id { get; set; }
public string Name { get; set; }
}
private void CmbList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DtgItems.ItemsSource = Parts.Where(t => t.Id == ((Bin)CmbList.SelectedItem).Id);
}
}

这里需要注意的是添加的事件处理程序CmbList_SelectionChanged,它在ComboBox选择更改时触发,它将更新网格的ItemsSource属性,使其仅为具有匹配Id的项。

您使用的是SelectedIndex属性,它与Id不同。索引是从零开始的,其中as id不是。

不用说,尽管代码有效,但还是有更好的方法来解决这个问题。我建议您研究MVVM模式和DataBinding,尽管代码隐藏方法在定义上也是有效的。祝你好运

相关内容

  • 没有找到相关文章

最新更新