如何将组合框单元格绑定到“拆分的"单元格? DataTabe列中的字符串 [英] How to bind ComboBox cell to "splitted" String in DataTabe Column
问题描述
大家好,
我无法弄清楚...例如,我有带有"ID"和名称"列的数据表"Table1".
行看起来像:
身份证名称
1个test1 | test2 | test3
2 test4 | test5 | test6
我这样绑定ID列:
Hi guys,
I can''t figured it out... For example I have DataTable "Table1" with "ID" and "Names" columns.
Rows look like:
ID Names
1 test1|test2|test3
2 test4|test5|test6
I bind ID column like this:
C#
...
DataSet dataset = new DataSet();
DataGridView.ItemSource = dataset.Table1.DefaultView;
...
XAML
...
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ID}" Header="ID" />
...
这部分效果很好.但是如何在我的情况下为名称"绑定组合框列?
请给我指示.谢谢.
[更新]
每行的名称"在DataSet中具有类似"test1 | test2 | test3"的值.
我正在寻找使用ComboBox在DataGridView行中从DataSet中显示值"test1 | test2 | test3"的解决方案,其中将包含以下项目:
-test1
-test2
-test3
让我知道是否还不清楚
Timur.
This part works well. But how can I bind combobox column for "Names" in my situation?
Please give me direction. Thank you.
[UPDATE]
"Names" has values like "test1|test2|test3" in DataSet for each row.
I''m looking for solution to display value "test1|test2|test3" from DataSet in DataGridView row with ComboBox which will have items:
- test1
- test2
- test3
Let me know if it''s still unclear
Timur.
推荐答案
在您的ViewModel中定义一个属性,例如:
In your ViewModel define a property like:
public IEnumerable<string> Names
{
get
{
return nameColumn.Split(''|'');
}
}
然后将您的ComboBox ItemsSource绑定到名称
好的,ViewModel将如下所示:
Then bind your ComboBox ItemsSource to Names
Ok, the ViewModel will look like:
public class MainViewModel
{
private string contentNames = "test1|test2|test3";
public IEnumerable<string> SplitContentNames
{
get
{
return contentNames.Split(''|'');
}
}
}
XAML如下所示:
The XAML will look like:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ComboBox ItemsSource="{Binding SplitContentNames}"/>
</Grid>
MainViewWindow.cs后面的代码将像这样绑定DtaContext:
And the code behind of MainViewWindow.cs will bind the DtaContext like so:
this.DataContext = new MainViewModel();
然后将所选项目放入视图模型,为所选项目添加一个属性:
Then to get the selected item into your view model, add a property for your selected item:
public string SelectedName
{
get;
set;
}
然后使用XAML绑定SelectedValue:
and then bind the SelectedValue using XAML:
<ComboBox ItemsSource="{Binding SplitContentNames}" SelectedValue="{Binding SelectedName}"/>
现在,当您更改组合框中的值时,将在SelectedName属性中设置当前值.
Now when you change the value in the combobox the current value will be set in the SelectedName property.
好的,这里表示的任何解决方案都不适合我.
我又自己解决了.解决方法如下:
创建类,如:
Okay, any solutions represented here do not work for me.
I solved by myself again. Here is the solution:
Create class like:
[ValueConversion(typeof(String), typeof(String))]
public class StringSplitterConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.ToString().Split('|');
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}
在组合框绑定中使用此转换器:
Use this converter in combobox binding:
...
<datagrid.resources>
<local:stringsplitterconverter x:key="splitterConverter" xmlns:x="#unknown" xmlns:local="#unknown" />
</datagrid.resources>
...
<datagridtemplatecolumn.celltemplate>
<datatemplate>
<combobox itemssource="{Binding Path=ContentNames, Converter={StaticResource splitterConverter}}"></combobox>
</datatemplate>
</datagridtemplatecolumn.celltemplate>
此解决方案效果很好.
This solution works great.
尝试一下
<datagridcomboboxcolumn header="Column Header" selecteditembinding="{Binding ID} DisplayMemberPath=" hold=" /></pre><br mode="></datagridcomboboxcolumn>
这篇关于如何将组合框单元格绑定到“拆分的"单元格? DataTabe列中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!