如何在XCeed DataGridControl(WPF)中添加ComboBox列 [英] How to add ComboBox column in XCeed DataGridControl (WPF)
问题描述
我正在尝试在XCeeds DataGridControl
中添加一个组合框列.设法制作了CellEditor
,它为绑定字段设置了适当的值,但是CellContent
模板存在问题.
I am trying to add a combobox column in XCeeds DataGridControl
. Managed to make a CellEditor
, which sets proper values to the binded field, but there are problems with the CellContent
template.
Xaml :
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<xcdg:DataGridControl ItemsSource="{Binding Address}" >
<xcdg:DataGridControl.Columns>
<xcdg:Column x:Name="clmAdd" FieldName="HouseNumberAdd"/>
<xcdg:Column x:Name="clmCity" FieldName="City"/>
<xcdg:Column x:Name="clmCountry" FieldName="CountryID">
<xcdg:Column.CellEditor>
<xcdg:CellEditor>
<xcdg:CellEditor.EditTemplate>
<DataTemplate>
<ComboBox SelectedValuePath="CountryID"
DisplayMemberPath="Name"
ItemsSource="{Binding Path=DataContext.Country, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
SelectedValue="{xcdg:CellEditorBinding}" IsEditable="True" Foreground="Black" IsSynchronizedWithCurrentItem="True" />
</DataTemplate>
</xcdg:CellEditor.EditTemplate>
</xcdg:CellEditor>
</xcdg:Column.CellEditor>
</xcdg:Column>
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
</Grid>
代码:
public partial class MainWindow : Window
{
ViewMode viewMode;
public MainWindow()
{
InitializeComponent();
viewMode = new ViewMode();
this.DataContext = viewMode;
}
private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DataTable source = viewMode.Address;
}
}
public class ViewMode
{
public DataTable Address { get; set; }
public DataTable Country { get; set; }
public ViewMode()
{
Address = new DataTable();
Address.Columns.Add("HouseNumberAdd", typeof(string));
Address.Columns.Add("City", typeof(string));
Address.Columns.Add("CountryID", typeof(int));
Address.Rows.Add("Ivlivensko 10-KV 1234", "Krakov", 1);
Address.Rows.Add("Astrakhanski 10-KV 1234", "Kharkiv", 2);
Address.Rows.Add("Tverskii 10-KV 1234", "Moskva", 3);
Address.Rows.Add("Klement 10-KV 1234", "Warsav", 1);
Country = new DataTable();
Country.Columns.Add("Name", typeof(string));
Country.Columns.Add("CountryID", typeof(int));
Country.Rows.Add("Poland", 1);
Country.Rows.Add("Ukrain", 2);
Country.Rows.Add("Russland", 3);
}
}
已:
我已经用ContentTemplate替换了CellEditor,但是当我尝试在Grid中编辑数据时,源表保持不变.我该如何解决?
I've replaced CellEditor by ContentTemplate, but when I am trying to edit the data inside Grid, the source table remains the same. How I can fix this?
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<xcdg:DataGridControl ItemsSource="{Binding Address}" >
<xcdg:DataGridControl.Columns>
<xcdg:Column x:Name="clmAdd" FieldName="HouseNumberAdd"/>
<xcdg:Column x:Name="clmCity" FieldName="City"/>
<xcdg:Column x:Name="clmCountry" FieldName="CountryID">
<xcdg:Column.CellContentTemplate>
<DataTemplate x:Name="clmCountryTmp">
<ComboBox SelectedValuePath="CountryID"
DisplayMemberPath="Name"
ItemsSource="{Binding Path=DataContext.Country, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
SelectedValue="{xcdg:CellEditorBinding}"/>
</DataTemplate>
</xcdg:Column.CellContentTemplate>
</xcdg:Column>
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
</Grid>
推荐答案
尝试删除IsSynchronizedWithCurrentItem="True"
在我的测试中,这样做阻止了文本值在进入编辑模式时出现在组合框中.删除后,文本将按预期显示.
In my tests, having this prevented the text value from appearing in the combobox when going in edit mode. As soon as I removed it, the text displayed as expected.
如果要在不处于编辑模式时更改单元格的外观,可以将自定义CellContentTemplate
分配给该列.
If you want to change the look of the cell when not in edit mode, you can assign a custom CellContentTemplate
to the column.
这篇关于如何在XCeed DataGridControl(WPF)中添加ComboBox列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!