C# 如何在XCeed DataGridControl(WPF)中添加ComboBox列
我试图在C# 如何在XCeed DataGridControl(WPF)中添加ComboBox列,c#,wpf,xaml,xceed,xceed-datagrid,C#,Wpf,Xaml,Xceed,Xceed Datagrid,我试图在XCeeds DataGridControl中添加一个组合框列。设法制作了一个CellEditor,它为绑定字段设置了适当的值,但是CellContent模板存在问题 Xaml: <Grid> <Grid.RowDefinitions> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinit
XCeeds DataGridControl
中添加一个组合框列。设法制作了一个CellEditor
,它为绑定字段设置了适当的值,但是CellContent
模板存在问题
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);
}
}
编辑::
<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>
<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”
在我的测试中,这样做可以防止文本值在进入编辑模式时出现在组合框中。我一删除它,文本就如预期的那样显示出来
如果要在不处于编辑模式时更改单元格的外观,可以为列指定自定义的
CellContentTemplate
。此外,如果希望单元格以文本形式显示组合框的显示值,请将以下内容添加到列中
<xcdg:Column.CellContentTemplate>
<DataTemplate>
<TextBlock Text="{xcdg:CellEditorBinding Converter={StaticResource YourConverter}}" />
</DataTemplate>
</xcdg:Column.CellContentTemplate>
其中“YourConverter”将所选的ValuePath值转换为组合框的DisplayMemberPath值。不清楚您有什么问题。Thx。我已经用ContentTemplate替换了CellEditor,但是当我编辑它时,源数据没有改变。你能帮我解决吗?找到了解决方法,CellEditorDisplayConditions=“Always”thx@Diane-Xceed在本例中,我希望单元格在不处于编辑模式时在DisplayMemberPath中显示值。CellContentTemplate应该如何编写才能做到这一点?@SezMe您可以使用一个TextBlock,该TextBlock在其绑定中使用转换器来转换列的值(例如Id),并返回要显示的字符串值。@Diane Xceed。是的,这就是我最后要做的。效率太低了,必须有更好的办法。