0

I have one object DataSet which has a name property and a ID property. I have a list of datasets in my viewmodel, which should be the itemsource of a DataGrid Combobox column. I have defined the binding as follows:

dataGridControl:DataGridComboBoxColumn
                                Binding="{Binding DataSetIndex}"
                                Header="DataSet"
                                ItemsSource="{x:Bind Model.DataSets}"
                                DisplayMemberPath="Name"
                                Tag="DataSet" />       
                           

Now, this works, except for the fact that I somehow need to tell the binding that it should use the DataSet.ID property. So when a user selects a value in the combobox, the binding DataSetIndex should be set to a int, which is the DataSet.ID property. How can I do that in the binding? Right now I of course get an error saying that the itemsource does not have a int property, cause it tries to use the DataSet object as a whole. Now, is there no SelectedValuePath in the community toolkit? Do I have to use a templatecolumn?

enter image description here

Here is my updated question based on your solution. It also has the codebehind binding.

 <dataGridControl:DataGridTemplateColumn Header="DataSet">
<dataGridControl:DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate x:DataType="local:DataExtractionSetupViewModel">
        <ComboBox x:Name="dataSetsComboBox" Loaded="ComboBox_Loaded"
                  DisplayMemberPath="Name"
                  SelectedValue="{Binding DataSetIndex, Mode=TwoWay}" SelectedValuePath="ID" IsDropDownOpen="True"/>
    </DataTemplate>
</dataGridControl:DataGridTemplateColumn.CellEditingTemplate>
<dataGridControl:DataGridTemplateColumn.CellTemplate>
    <DataTemplate> 
    <TextBlock Text="{Binding DatasetIndex}"/>
</DataTemplate>
</dataGridControl:DataGridTemplateColumn.CellTemplate>
</dataGridControl:DataGridTemplateColumn>

Now my only question is, how can we make this custom template column look exactly like a normal datagrid combobox column, and also show the datasets name in the textbox instead of the index?

1
  • if the solution is to create a custom template column, I have 2 questions. #1 how can I refer to Model.DataSets when the Model is defined and newed in the code behind when using DataTemplate in the custom column? #2 how can one make a custom templatecolumn with a combobox behave the same as the original DataGrid Combobox column? Commented Jan 6 at 17:19

1 Answer 1

1

The following is using code-behind but works:

<toolkit:DataGridTemplateColumn>
    <toolkit:DataGridTemplateColumn.CellTemplate>
        <DataTemplate x:DataType="local:DataSet">
            <ComboBox
                DisplayMemberPath="Name"
                Loaded="ComboBox_Loaded"
                SelectedIndex="{x:Bind DataSetIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>
private void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
    if (sender is not ComboBox comboBox)
    {
        return;
    }

    comboBox.SetBinding(
        ComboBox.ItemsSourceProperty,
        new Binding()
        {
            Source = Model,
            Path = new PropertyPath("DataSets"),
        });
}

If you want to change how the ComboBox looks, try setting:

  • CornerRadius to 0
  • BorderThickness to 0
  • Background to Transparent
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for the reply. For the cell to look exactly like a normal datagrid combobox column, shouldn't it have a editing template and then a non editing template with a textbox?
also, it seems like the binding is missing something. DataSetIndex is in the ViewModel.Documents observablecollection this datagrid is made from, and DataSets is a observablecollection in the ViewModel. Also it doesn't have to be in code behind, I just said that the viewmodel is defined in codebehind.
Ok, so I tesdted your suggestions. Binding in codebehind defeintely solves the issue of the binding not working when doing exactly the same in XAML. I guess it just isn't possible in XAML due to a timing issue? I have tried both x:bind and binding.
AFAIK, this is the only way that works now.
Ok, it works great now. The only issue is how I can make the textbox show the value. See my updated question for the code. I guess one approach could be to bind to the combobox selected value or something?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.