获取DataGrid中ComboBox的选定值

发布于 2024-10-28 12:17:00 字数 3273 浏览 1 评论 0原文

如何获取数据网格中组合框的选定值?

我已经简化了我的问题,以便别人更容易提供帮助。这是 XAML:

<Window x:Class="del_WpfApplication3.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>
        <DataGrid DataContext="{Binding}" AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="60,32,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="368">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding test}"></DataGridTextColumn>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding combo}" DisplayMemberPath="value" SelectedValuePath="key" SelectedItem="{Binding selected, Mode=TwoWay}"></ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="353,238,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </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;

namespace del_WpfApplication3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            load();
        }

        public class tmp1
        {
            public string test { get; set; }
            public IList<tmp2> combo { get; set; }
            public string selected { get; set; }
        }

        public class tmp2
        {
            public string key { get; set; }
            public string value { get; set; }
            public string selected { get; set; }
        }

        private void load()
        {
            IList<tmp2> list = new List<tmp2>();
            list.Add(new tmp2(){ key = "key1", value = "value1" });
            list.Add(new tmp2(){ key = "key2", value = "value2" });

            IList<tmp1> list2 = new List<tmp1>();
            list2.Add(new tmp1() { test = "test1", combo = list });
            list2.Add(new tmp1() { test = "test2", combo = list });

            dataGrid1.ItemsSource = list2;


        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            tmp1 t = (tmp1)dataGrid1.Items[0];
            Console.WriteLine(t.selected); // this is empty when I'm expecting it to be the selected value as set in the XAML.
        }
    }
}

How can I get the selected value of a combo box that is in a datagrid?

I've simplefied my problem so that it is easier for someone to help. Here is the XAML:

<Window x:Class="del_WpfApplication3.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>
        <DataGrid DataContext="{Binding}" AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="60,32,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="368">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding test}"></DataGridTextColumn>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding combo}" DisplayMemberPath="value" SelectedValuePath="key" SelectedItem="{Binding selected, Mode=TwoWay}"></ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="353,238,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

Here is the code behind:

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;

namespace del_WpfApplication3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            load();
        }

        public class tmp1
        {
            public string test { get; set; }
            public IList<tmp2> combo { get; set; }
            public string selected { get; set; }
        }

        public class tmp2
        {
            public string key { get; set; }
            public string value { get; set; }
            public string selected { get; set; }
        }

        private void load()
        {
            IList<tmp2> list = new List<tmp2>();
            list.Add(new tmp2(){ key = "key1", value = "value1" });
            list.Add(new tmp2(){ key = "key2", value = "value2" });

            IList<tmp1> list2 = new List<tmp1>();
            list2.Add(new tmp1() { test = "test1", combo = list });
            list2.Add(new tmp1() { test = "test2", combo = list });

            dataGrid1.ItemsSource = list2;


        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            tmp1 t = (tmp1)dataGrid1.Items[0];
            Console.WriteLine(t.selected); // this is empty when I'm expecting it to be the selected value as set in the XAML.
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以畏孤独 2024-11-04 12:17:00

这是一个我使用 Telerik 控件的示例,但这并不重要。

 <telerik:RadComboBox Margin="5" 
        Height="28" Width="220" 
        VerticalAlignment="Top" HorizontalAlignment="Left"
        ItemsSource="{Binding Path=ProductTypeList}"
        SelectedItem="{Binding Path=SelectedProductType, Mode=TwoWay}"
        DisplayMemberPath="Code">
  </telerik:RadComboBox>

请注意,我只是将所选项目设置为与该类型的对象的绑定...所以这意味着我可以访问整个对象,并且我可以在我选择的任何地方查看并查看列表在哪里等等...重点在于,您不必跟踪对象集合的一个属性,而是跟踪该集合中的整个对象。

Here is an example where I am using a telerik control but that matters not.

 <telerik:RadComboBox Margin="5" 
        Height="28" Width="220" 
        VerticalAlignment="Top" HorizontalAlignment="Left"
        ItemsSource="{Binding Path=ProductTypeList}"
        SelectedItem="{Binding Path=SelectedProductType, Mode=TwoWay}"
        DisplayMemberPath="Code">
  </telerik:RadComboBox>

Notice, that I just set the selected item to a binding with an object of that type... So what this means is that I have access to that whole entire object, and I can anywhere I choose look and see where is the list it is etc... The whole point is instead of tyring to keep track of one property of the object collection, you keep track of the whole object in that collection.

稚气少女 2024-11-04 12:17:00

除非 ItemsSource 绑定到的“content”属性中有一些奇怪的嵌套属性,否则我猜测 ComboBox 甚至不显示数据。

Combobox 应该定义为:

<ComboBox 
  ItemsSource="{Binding content}" 
  DisplayMemberPath="ContentName" 
  SelectedValuePath="ContentID" 
  Name="ContentSelector" />

其中 ContentNameContentIDcontent 的属性,它被定义为 <代码>ItemsSource。

Unless there is some strange nested properties in the "content" property that the ItemsSource is bound to, I'm guessing the ComboBox isn't even displaying data.

The Combobox should likely be defined as:

<ComboBox 
  ItemsSource="{Binding content}" 
  DisplayMemberPath="ContentName" 
  SelectedValuePath="ContentID" 
  Name="ContentSelector" />

Where ContentName and ContentID are properties of content which is defined as the ItemsSource.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文