【问题标题】:WPF DataGrid binding to list and sub-listWPF DataGrid 绑定到列表和子列表
【发布时间】:2021-08-26 10:50:27
【问题描述】:

我有一个Bin 对象列表,其中包含Id、Name 和List<int> 属性,我将其加载为ComboBox 的ItemsSource,以及带有List<Part> 的DataGrid具有Id 和Name 属性的对象。

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

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

MainWindow.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>

MainWindow.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; }

    }
}

【问题讨论】:

    标签: c# xaml binding datagrid


    【解决方案1】:

    这是一个工作示例:

    <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 不同。索引是从零开始的,而 id 不是。

    不用说,尽管代码有效,但还有更好的方法来解决这个问题。我建议您查看MVVM pattern 和DataBinding,尽管代码隐藏方法也绝对有效。祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 2021-03-18
      • 2021-04-11
      • 2023-03-24
      • 2014-08-13
      • 1970-01-01
      相关资源
      最近更新 更多