我确定有人会在其他地方回答此问题,但我声称我已经关注了本网站(及其他地方)上的每个相关链接,并尝试了ItemTemplate,ComboBoxItem,ItemContainerStyle,DisplayMemberPath,SelectedItem的所有可能组合,SelectedValuePath和SelectedValue,在我的生命中,我无法按需使用它。
我只是想在XAML资源中定义ContentControl对象的列表(使用x:Array),用作ComboBox的ItemsSource。此列表包含2个元素,它们的Content属性设置为Grid控件-也在资源中定义。每一项还具有设置为字符串的Tag属性,该字符串用作内容的名称或描述。
非常简单:我希望Tag属性中的字符串显示在ComboBox的选定项部分中,并显示为ComboBox下拉部分中的可选项列表。
如果我设置DisplayMemberPath =“ Tag”,则下拉部分将按预期工作,但在ComboBox的选定项目部分中将不显示任何内容。我确定正确使用SelectedValuePath和SelectedValue是关键,但是我无法使其正常工作。
<UserControl.Resources>
<Grid x:Key="Fluidics" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="134"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="Cartridge:"
VerticalAlignment="Center" HorizontalAlignment="Left"
Margin="0,5,0,0" Height="26" Width="62"/>
<ComboBox Grid.Column="1"
ItemsSource="{Binding Cartridges}"
SelectedIndex="{Binding SelectedCartridgeIndex, FallbackValue=0}"
SelectedItem="{Binding SelectedCartridge}"
Margin="0,5,0,0" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Config.FriendlyName}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<views:FluidicsExperimentParamsView Grid.Row="1" Grid.ColumnSpan="2" DataContext="{Binding FluidicsExperimentParams}" Margin="0,10,0,0"/>
</Grid>
<Grid x:Key="Data acquisition" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="157"/>
<ColumnDefinition Width="Auto" MinWidth="87"/>
</Grid.ColumnDefinitions>
<Label Content="Camera line:" />
<ComboBox
Grid.Column="1"
IsReadOnly="True"
ItemsSource="{Binding ImageDataAcquisitionParams}"
SelectedIndex="{Binding SelectedCameraLine}"
SelectedItem="{Binding SelectedImageDataAcquisitionParams}"/>
<data:ImageDataAcquisitionView
Grid.Row="1"
Grid.ColumnSpan="2"
DataContext="{Binding ImageDataAcquisition}" Margin="0,10,0,0"/>
</Grid>
<x:Array x:Key="ControlActivities" Type="ContentControl">
<ContentControl Content="{Binding Source={StaticResource Fluidics}}" Tag="Fluidics" />
<ContentControl Content="{Binding Source={StaticResource Data acquisition}}" Tag="Data acquisition"/>
</x:Array>
</UserControl.Resources>
...
<ComboBox Name="ControlActivityComboBox"
MinWidth="150"
ItemsSource="{Binding Source={StaticResource ControlActivities}}"
SelectedItem="{Binding SelectedActivity}"
SelectedValuePath="Tag"
SelectedValue="{Binding SelectedActivity.Tag}"
DisplayMemberPath="Tag"
HorizontalAlignment="Left"
SelectedIndex="0">
</ComboBox>