0

I have a multi-column ComboBox with headers. I have got that to work by using this answer.

Here is the XAML that I have used :

<CollectionViewSource x:Key="GroupNamesWithCorrespondingEffectsCollection" Source="{Binding GroupNamesWithCorrespondingEffects}" />

<CompositeCollection x:Key="Items">
    <ComboBoxItem IsEnabled="False" Background="#FF2A2A2A" Foreground="White">
        <Grid TextElement.FontWeight="Bold" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="A" />
                <ColumnDefinition Width="50" />
                <ColumnDefinition SharedSizeGroup="B" />
            </Grid.ColumnDefinitions>
            <Grid.Children>
                <TextBlock Grid.Column="0" Text="Group Name" />
                <TextBlock Grid.Column="2" Text="Effect" />
            </Grid.Children>
        </Grid>
    </ComboBoxItem>
    <CollectionContainer Collection="{Binding Source={StaticResource GroupNamesWithCorrespondingEffectsCollection}}" />
</CompositeCollection>

<DataTemplate DataType="{x:Type helpers:GroupNameWithCorrespondingEffect}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition SharedSizeGroup="A" />
            <ColumnDefinition Width="50" />
            <ColumnDefinition SharedSizeGroup="B" />
        </Grid.ColumnDefinitions>
        <Grid.Children>
            <TextBlock Grid.Column="0" Text="{Binding GroupName}" />
            <TextBlock Grid.Column="2" Text="{Binding CorrespondingEffect}" />
        </Grid.Children>
    </Grid>
</DataTemplate>

<ComboBox ItemsSource="{DynamicResource Items}" 
          SelectedValue="{Binding GroupNameWithCorrespondingEffect}"
          SelectedValuePath="GroupID"
          DisplayMemberPath="GroupName" />

Note:

C# code is not posted as I think it is not necessary to post here. If anybody wants to have a look at C# code please tell me & I will post it.

Problems:

I want to check the SelectedIndex of ComboBox in CodeBehind file. But I noticed that the SelectedIndex always remains -1. What might be the problem? And how should I overcome it?

Community
  • 1
  • 1
Vishal
  • 6,238
  • 10
  • 82
  • 158

1 Answers1

1

You have binded SelectedValue to GroupNameWithCorrespondingEffect which I suspect is of type GroupNameWithCorrespondingEffect and at same time binded SelectedValuePath to GroupID which either be an int or uint.

SelectedValue and SelectedValuePath should always be of same type. In your case you can remove the SelectedValuePath and bind directly with SelectedValue.

<ComboBox ItemsSource="{DynamicResource Items}" 
          SelectedValue="{Binding GroupNameWithCorrespondingEffect}"
          DisplayMemberPath="GroupName" />
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • My combobox resides inside datagridtemplatecolumn. GroupID is an integer which is a property of GroupNameWithCorrespondingEffect. If i change ItemsSource from {DynamicResource Items} to {Binding DataContext.GroupNamesWithCorrespondingEffects, RelativeSource={RelativeSource Mode=FindAncestor AncestorType={x:Type Page}}} then i get correct SelectedIndex. – Vishal Jul 20 '14 at 07:31
  • Can you post where you are trying to get SelectedIndex OR may be a small sample piece of code to replicate your issue? – Rohit Vats Jul 20 '14 at 07:37
  • I am trying to get SelectedIndex in PreviewKeyDown event when I press Enter key. I don't have my laptop with me at this tume, so I can't post the code right now. – Vishal Jul 20 '14 at 07:42
  • I have created a simple sample project where everything works fine. Now I suspect that my SelectedValue Binding might be incorrect. My ComboBox resides inside a DataGridTemplateColumn's CellEditingTemplate.Suppose ComboBox is bound to the Person Object having three properties namely ID, Name and Age, then to which property should my SelectedValue of the ComboBox bound?? I am very much confused when binding a ComboBox which resides inside a DataGrid. Can you explain it with a small example or any link to a good tutorial? – Vishal Jul 20 '14 at 13:45
  • That's completely depends on your choice of `SelectedValuePath`. Like I mentioned in answer, `SelectedValuePath` and `SelectedValue` should be of same type always. In case you haven't set SelectedValuePath, SelectedValue type should of same type as that of collection of ItemsSource. So, in your case if you have set `SelectedValuePath` to `ID`, SelectedValue should bind to property of type int and in case `SelectedValuePath` is not set, it should be of type `Person`. – Rohit Vats Jul 20 '14 at 14:13
  • I understand that it should be of type person if I do not set SelectedValuePath. Then where should I declare an Instance of Person object, If I declare it in ViewModel then the problem will be such that all the ComboBoxes will be bound to a single Property and so Selection of all the ComboBoxes will change on changing selection of one ComboBox. Should I do something like this : `SelectedValue={Binding Person}`? – Vishal Jul 20 '14 at 14:24
  • Selected property should not be there in ViewModel but instead in model class i.e. class which represents single row of dataGrid i.e. if ItemsSource of dataGrid is bound to collection of class Manager than SelectedValue property should reside in Manager class. – Rohit Vats Jul 20 '14 at 14:35
  • when I use general comboBox then my application works correctly you can see it here: http://stackoverflow.com/a/24885559/2284240, as soon as I change the ItemsSource to "{DynamicResource Items}" selection does not work correctly By changing the ItemsSource I want my comboBox to show multiple Columns with header. – Vishal Jul 22 '14 at 13:53
  • SelectedValue in this case Should be ParentID and SelectedValuePath should be GroupID. Thanks for your time and patience. – Vishal Jul 28 '14 at 11:00