1

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>

Code:

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);
    }
}

EDITED::

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>
v.chjen
  • 615
  • 1
  • 8
  • 20

2 Answers2

2

Try removing 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.

If you want to change the look of the cell when not in edit mode, you can assign a custom CellContentTemplate to the column.

Diane-Xceed
  • 319
  • 1
  • 6
  • Thx. I've replaced the CellEditor by ContentTemplate, but when I edit it, the source data doesn't change. Could you please help me with that? – v.chjen May 20 '16 at 08:41
  • found how to fix that, added CellEditorDisplayConditions="Always" thx – v.chjen May 20 '16 at 09:22
  • @Diane-Xceed In this example, I'd like the cell to display the value in the DisplayMemberPath when the cell is not in edit mode. How should the CellContentTemplate be written to do that? – SezMe Apr 15 '18 at 03:49
  • @SezMe You could use a TextBlock that uses a Converter in its Binding to convert the column's value (an Id for example) and returns the string value that you want to display. – Diane-Xceed Apr 30 '18 at 16:53
  • @Diane-Xceed. Yeah, that's what I have ended up doing. It's terribly inefficient., There has to be a better way. – SezMe May 01 '18 at 19:31
0

In addition, if you want the cell to show the DisplayValue of the ComboBox in text form, add the following to your column.

<xcdg:Column.CellContentTemplate>
    <DataTemplate>
        <TextBlock Text="{xcdg:CellEditorBinding Converter={StaticResource YourConverter}}" />
    </DataTemplate>
</xcdg:Column.CellContentTemplate>

where "YourConverter" converts the SelectedValuePath value into the DisplayMemberPath value of the ComboBox.

SezMe
  • 527
  • 8
  • 24