Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将枚举数或变量绑定到WPF中DataGrid中的DataGridComboBox?_Wpf_Binding_Wpfdatagrid - Fatal编程技术网

如何将枚举数或变量绑定到WPF中DataGrid中的DataGridComboBox?

如何将枚举数或变量绑定到WPF中DataGrid中的DataGridComboBox?,wpf,binding,wpfdatagrid,Wpf,Binding,Wpfdatagrid,我想将枚举器、字符串数组或特定DataTable的内容作为DatagridComboBox的项,那么如何将枚举器、字符串数组或DataTable内容绑定到DatagridComboBox 例如,我有一个Datatable,我将加载到DataGrid,并将记录绑定到自定义列,并且根据单元格(来自Datatable)的值,当列(在DataGrid中)是DataGridComboBox时,它将自动选择DataGridComboBox的相应项 将列绑定为DataGridTextBox很容易,但将列绑定为

我想将枚举器、字符串数组或特定DataTable的内容作为DatagridComboBox的项,那么如何将枚举器、字符串数组或DataTable内容绑定到DatagridComboBox

例如,我有一个Datatable,我将加载到DataGrid,并将记录绑定到自定义列,并且根据单元格(来自Datatable)的值,当列(在DataGrid中)是DataGridComboBox时,它将自动选择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;
        }
    }
}
命名空间WpfApplication1
{
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
ObservableCollection集合=新的ObservableCollection();
Add(newtestclass());
Add(newtestclass());
Add(newtestclass());
dataGrid.DataContext=集合;
}
}
公共类TestClass
{
私有静态字符串[]stringArray={“选项一”、“选项二”、“选项三”};
公共字符串[]字符串数组
{
得到
{
返回字符串数组;
}
}
公共字符串SelectedString
{
得到;
设置
}
}
}
您需要将窗口/控件的datacontext设置为后面的某些数据,然后可以使用这些对象的属性绑定控件


我希望这会有所帮助

,但我还有另一个问题,如何从XAML中的代码隐藏中访问变量?我已经找到了一些例子,但无论如何,我无法理解这一切都在datacontext中,为了将设置网格的datacontext(或网格的某个父级)所需的textcolumns绑定到datatable中。所有其他控件/列也是如此-您只需要设置datacontext的相应属性的路径。在ComboBox中选择我已经理解的值,但将ComboBox与项绑定,我真的无法理解,因为我不知道如何从XAML访问datacontext中的任何内容。我看到了您的示例,看起来很简单,但我不能按照您的方式来做,因为如果我尝试在ItemsSource中设置绑定的路径,他不会识别我的变量。请编辑答案以提供更好的示例。希望有帮助