I have followed the accepted answer of this question to define a multi-column combobox with headers.
It was not working for me, so I made some changes to it and now it works. Here is the xaml to create a multi-column comboBox with headers.
<Page.DataContext>
<vm:ItemsViewModel />
</Page.DataContext>
<Page.Resources>
<CollectionViewSource x:Key="UnitsCollection" Source="{Binding Units}" />
</Page.Resources>
<ComboBox Grid.Row="0" Grid.Column="4" Grid.ColumnSpan="2" Grid.IsSharedSizeScope="True"
x:Name="cbUnits" ItemsSource="{DynamicResource Items}" IsEditable="True" TextSearch.TextPath="Symbol"
SelectedValue="{Binding SelectedUnit}" SelectedValuePath="UnitID">
<ComboBox.Resources>
<CompositeCollection x:Key="Items">
<ComboBoxItem IsEnabled="False">
<Grid TextElement.FontWeight="Bold">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" />
<ColumnDefinition Width="50" />
<ColumnDefinition SharedSizeGroup="B" />
</Grid.ColumnDefinitions>
<Grid.Children>
<TextBlock Grid.Column="0" Text="Name" />
<TextBlock Grid.Column="2" Text="Symbol" />
</Grid.Children>
</Grid>
</ComboBoxItem>
<Separator />
<CollectionContainer Collection="{Binding Source={StaticResource UnitsCollection}}" />
</CompositeCollection>
<DataTemplate DataType="{x:Type models:Unit}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" />
<ColumnDefinition Width="50" />
<ColumnDefinition SharedSizeGroup="B" />
</Grid.ColumnDefinitions>
<Grid.Children>
<TextBlock Grid.Column="0" Text="{Binding Name}" />
<TextBlock Grid.Column="2" Text="{Binding Symbol}" />
</Grid.Children>
</Grid>
</DataTemplate>
</ComboBox.Resources>
</ComboBox>
</Page>
Now, I would like to define the section between <ComboBox.Resources>
in a resource dictionary, so that I would not have to write it again and again. I also want to add two more functionalities to the resource dictionary:
Bind the Column-names and the Column-values somehow to something(I don't know), so that any two column combobox can use this resource dictionary.
If possible I would like to add some logic in resource dictionary so that the same resource dictionary can be used for any combobox with any number of columns.
I think somebody could guide me to the right direction and if someone has done this thing in past then I may get some help for that, which is another intension of asking this question.
If my above quesitons are not solved using only xaml, then also I would happily accept the solution.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…