如何在WPF中将枚举数或变量绑定到DataGrid中的DataGridComboBox? [英] How to bind an enumerator or variable to a DataGridComboBox in DataGrid in WPF?

查看:95
本文介绍了如何在WPF中将枚举数或变量绑定到DataGrid中的DataGridComboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将枚举器的内容或某些字符串数组或特定的DataTable作为DatagridComboBox的项,因此如何将枚举器或字符串数​​组或DataTable内容绑定至DataGridComboBox?/p>

例如,我有一个数据表,我将加载到DataGrid并将记录绑定到自定义列,并且当列(在DataGrid中)为DataGridComboBox时,根据单元格的值(来自Datatable),它将自动选择DataGridComboBox的对应项.

将列绑定为DataGridTextBox很容易,但是将列绑定为DataGridComboBox似乎很令人困惑.

我的第一个问题是将项目从(枚举器,字符串数组或Datatable或其他对象)放入DataGridComboBox,第二个问题是当我加载包含记录的DataTable时选择相应的项目到DataGrid.

预先感谢

解决方案

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"    
    Title="Window1" Height="300" Width="300">
    <StackPanel>

        <toolkit:DataGrid Name="dataGrid" ItemsSource="{Binding Path=.}" AutoGenerateColumns="False">
            <toolkit:DataGrid.Columns>
                <toolkit:DataGridTemplateColumn>
                    <toolkit:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=StringArray}" Width="100"
                                      SelectedValue="{Binding Path=SelectedString}" />
                        </DataTemplate>
                    </toolkit:DataGridTemplateColumn.CellTemplate>   
                </toolkit:DataGridTemplateColumn>
            </toolkit:DataGrid.Columns>
        </toolkit:DataGrid>


    </StackPanel>
</Window>

在您后面的代码中:

    namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            ObservableCollection<TestClass> collection = new ObservableCollection<TestClass>();
            collection.Add(new TestClass());
            collection.Add(new TestClass());
            collection.Add(new TestClass());

            dataGrid.DataContext = collection;
        }
    }

    public class TestClass
    {
        private static string[] stringArray = { "Option One", "Option Two", "Option Three" };

        public string[] StringArray
        {
            get
            {
                return stringArray;
            }
        }

        public string SelectedString
        {
            get;
            set;
        }
    }
}

您需要将窗口/控件的数据上下文设置为后面的一些数据,然后才能使用这些对象的属性来绑定控件.

我希望这对您有帮助

I want to put the contents of an enumerator, or some array of strings or a specific DataTable as the items of a DatagridComboBox, so how i can bind the enumerator or the array of strings or a DataTable contents to a DataGridComboBox?

For example, i have a Datatable i will load to a DataGrid and will bind the records to customized columns, and depending on value of the cell (from the Datatable) when the column (in the DataGrid) is a DataGridComboBox it will auto-select the corresponding item of the DataGridComboBox.

Binding columns as DataGridTextBox is easy, but columns as DataGridComboBox seems to be confusing.

My first problem is to put the items from (an Enumerator, or an array of strings, or a Datatable or whatever) to the DataGridComboBox, and the second problem is select the corresponding item when i load the DataTable which contains the records to the DataGrid.

Thanks in advance

解决方案

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"    
    Title="Window1" Height="300" Width="300">
    <StackPanel>

        <toolkit:DataGrid Name="dataGrid" ItemsSource="{Binding Path=.}" AutoGenerateColumns="False">
            <toolkit:DataGrid.Columns>
                <toolkit:DataGridTemplateColumn>
                    <toolkit:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=StringArray}" Width="100"
                                      SelectedValue="{Binding Path=SelectedString}" />
                        </DataTemplate>
                    </toolkit:DataGridTemplateColumn.CellTemplate>   
                </toolkit:DataGridTemplateColumn>
            </toolkit:DataGrid.Columns>
        </toolkit:DataGrid>


    </StackPanel>
</Window>

In your code behind:

    namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            ObservableCollection<TestClass> collection = new ObservableCollection<TestClass>();
            collection.Add(new TestClass());
            collection.Add(new TestClass());
            collection.Add(new TestClass());

            dataGrid.DataContext = collection;
        }
    }

    public class TestClass
    {
        private static string[] stringArray = { "Option One", "Option Two", "Option Three" };

        public string[] StringArray
        {
            get
            {
                return stringArray;
            }
        }

        public string SelectedString
        {
            get;
            set;
        }
    }
}

You need to set the datacontext of the window/control to some data behind and then you can use the properties of those objects to bind your controls.

I hope this helps

这篇关于如何在WPF中将枚举数或变量绑定到DataGrid中的DataGridComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆