如何设置一个DataTable是一个DataGridComboBoxColum的的ItemsSource? [英] How i set a DataTable to be the ItemsSource of a DataGridComboBoxColum?

查看:215
本文介绍了如何设置一个DataTable是一个DataGridComboBoxColum的的ItemsSource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两列数据表中,我将绑定到DataGridComboBoxColumn的所有组合框,其中一列将是项目的文本和其他将是项的值。

我知道那个叫的DisplayMemberPath'属性是我指定列的名称是项的文本,并向其价值已经在SelectedValuePath'属性来指定。

但有更多的时间对任何绑定到一个DataGridComboBoxColumn,因为它无法访问的DataGrid DataContext的一个问题。

那么,如何设置一个DataTable是一个DataGridComboBoxColum的的ItemsSource?

例子,我想在code作品背后:

  TestClass的测试=新识别TestClass();dataGrid.Columns.Add(新DataGridComboBoxColumn()
{
    标题=城市,
    的DisplayMemberPath =城市,
    SelectedValuePath =ID中,
    的ItemsSource = test.Dt.DefaultView,
});

下面是我的XAML code:

 <窗​​口x:类=WpfApp3.MainWindow
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
    XMLNS:地方=CLR的命名空间:WpfApp3
    标题=主窗口HEIGHT =350WIDTH =600>    <电网NAME =GRID1>
        < D​​ataGrid的名称=的数据网格的AutoGenerateColumns =FALSEMinColumnWidth =100>
            < D​​ataGrid.Columns>
                < D​​ataGridComboBoxColumn标题=城的DisplayMemberPath =城市SelectedValuePath =ID的ItemsSource ={结合地方:TestClass.Dt}/>
            < /DataGrid.Columns>
        < / DataGrid的>
    < /网格和GT;
< /窗GT;

下面是我的code code背后:

 公共类识别TestClass
{
    公众识别TestClass()
    {
        (......)
        //这里我加载我的DataTable
    }    私有静态数据表DT;    公共静态数据表申
    {
        {返回DT; }
    }
}


解决方案

不是,


A.明确地设置的DataContext 组合框到您的数据表,OR


B.如果数据表是你已经绑定到DataGrid对象的某些部分,可以使用的RelativeSource 在组合框的结合。

您需要发布您的code,如果你需要这些方案具体的帮助。

I have a DataTable with two Columns that i will bind to all the ComboBoxes of a DataGridComboBoxColumn where one of the Columns will be the Text of the Items, and other will be the Values of the Items.

I know that the property called 'DisplayMemberPath' is where i specify the Name of the Column to be the Text of the Items, and to the values it have to be specified in the 'SelectedValuePath' property.

But one more time it's a problem to Bind whatever to a DataGridComboBoxColumn because it can't access the DataContext of the DataGrid.

So how i set a DataTable to be the ItemsSource of a DataGridComboBoxColum?

Example where what i want works in Code Behind:

TestClass test = new TestClass();

dataGrid.Columns.Add(new DataGridComboBoxColumn()
{
    Header = "City",
    DisplayMemberPath = "Cities",
    SelectedValuePath = "ID",
    ItemsSource = test.Dt.DefaultView,
});

Here is my XAML Code:

<Window x:Class="WpfApp3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApp3
    Title="MainWindow" Height="350" Width="600">

    <Grid Name="grid1">
        <DataGrid Name="dataGrid" AutoGenerateColumns="False" MinColumnWidth="100">
            <DataGrid.Columns>
                <DataGridComboBoxColumn Header="City" DisplayMemberPath="Cities" SelectedValuePath="ID" ItemsSource="{Binding local:TestClass.Dt}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Here is my Code Behind code:

public class TestClass
{
    public TestClass()
    {
        (...)
        //Here i am loading my DataTable
    }

    private static DataTable dt;

    public static DataTable Dt
    {
        get { return dt; }
    }
}

解决方案

Either,
A. explicity set the DataContext of ComboBox to your DataTable, OR
B. if DataTable is some part of the object that you have bound to the DataGrid, use RelativeSource in ComboBox binding.

You will need to post your code, if you need specific help for these scenarios.

这篇关于如何设置一个DataTable是一个DataGridComboBoxColum的的ItemsSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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