如何在XCeed DataGridControl(WPF)中添加ComboBox列

时间:2016-05-19 11:27:37

标签: c# wpf xaml xceed xceed-datagrid

我正在尝试在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);
    }
}

编辑:

我已经通过ContentTemplate替换了CellEditor,但是当我尝试编辑Grid中的数据时,源表保持不变。我怎么解决这个问题?

 <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>

2 个答案:

答案 0 :(得分:2)

尝试删除IsSynchronizedWithCurrentItem="True"

在我的测试中,这样可以防止文本值在编辑模式下出现在组合框中。一旦我删除它,文本就会按预期显示。

如果您想在不处于编辑模式时更改单元格的外观,可以为该列指定自定义CellContentTemplate

答案 1 :(得分:0)

此外,如果您希望单元格以文本形式显示ComboBox的DisplayValue,请将以下内容添加到列中。

FragmentManager

其中“YourConverter”将SelectedValuePath值转换为ComboBox的DisplayMemberPath值。