如何将 DataTable 设置为 DataGridComboBoxColum 的 ItemsSource?
我有一个包含两列的 DataTable,我将它们绑定到 DataGridComboBoxColumn 的所有组合框,其中一列将是项目的文本,其他列将是项目的值。
我知道名为“DisplayMemberPath”的属性是我将列的名称指定为项目文本的位置,并且必须在“SelectedValuePath”属性中指定它的值。
但是,将任何内容绑定到 DataGridComboBoxColumn 又会出现问题,因为它无法访问 DataGrid 的 DataContext。
那么我如何将 DataTable 设置为 DataGridComboBoxColum 的 ItemsSource ?
我想要在代码隐藏中工作的示例:
TestClass test = new TestClass();
dataGrid.Columns.Add(new DataGridComboBoxColumn()
{
Header = "City",
DisplayMemberPath = "Cities",
SelectedValuePath = "ID",
ItemsSource = test.Dt.DefaultView,
});
这是我的 XAML 代码:
<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>
这是我的代码隐藏代码:
public class TestClass
{
public TestClass()
{
(...)
//Here i am loading my DataTable
}
private static DataTable dt;
public static DataTable Dt
{
get { return dt; }
}
}
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; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
任何一个,
A. 显式地将 ComboBox 的 DataContext 设置为您的 DataTable,或者
B. 如果 DataTable 是已绑定到 DataGrid 的对象的某些部分,请在 ComboBox 绑定中使用
RelativeSource
。如果您需要针对这些场景的特定帮助,您将需要发布您的代码。
Either,
A. explicity set the
DataContext
of ComboBox to your DataTable, ORB. 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.