【问题标题】:How to bind an enumerator or variable to a DataGridComboBox in DataGrid in WPF?如何将枚举数或变量绑定到 WPF 中 DataGrid 中的 DataGridComboBox?
【发布时间】:2011-03-24 10:28:25
【问题描述】:

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

例如,我有一个 Datatable,我将加载到 DataGrid 并将记录绑定到自定义列,并且当列(在 DataGrid 中)是 DataGridComboBox 时,取决于单元格的值(来自 Datatable)自动选择DataGridComboBox的对应项。

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

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

提前致谢

【问题讨论】:

    标签: wpf binding wpfdatagrid


    【解决方案1】:
    <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;
            }
        }
    }
    

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

    希望对你有帮助

    【讨论】:

    • 但是我有另一个问题,我如何在 XAML 中从 Code-Behind 访问变量?我找到了一些例子,但无论如何我无法理解
    • 这一切都在数据上下文中,要绑定甚至需要将网格(或网格的某些父级)的数据上下文设置到数据表的文本列。所有其他控件/列也是如此 - 您只需将 Path 设置为 datacontext 的适当属性。
    • 在我已经理解的 ComboBox 中选择值,但是将 ComboBox 与项目绑定,我真的无法理解,因为我不知道如何从 XAML 访问 DataContext 中的任何内容。我看到了你的例子,看起来很简单,但我不能按照你的方式来做,因为如果我尝试在 ItemsSource 中设置绑定的路径,他就无法识别我的变量。
    • 编辑了答案以提供更好的示例。希望有帮助
    猜你喜欢
    • 2019-11-30
    • 2015-07-03
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    相关资源
    最近更新 更多