WPF组合框所选项目未显示其值

本文关键字:显示 项目 选项 组合 WPF | 更新日期: 2023-09-27 18:12:30

我已经创建了WPF应用程序。我有一个颜色组合框。我想要选择的颜色作为我的ComboBoxItem,但它显示的是system。windows。controls。当我从ComboBox中选择一个项目时,ComboBoxItem'而不是一个项目(这是我在这里的颜色名称)。

下面是ComboBox的xaml代码:
<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,330,557,0" Name="comboBox_PC_Opt" VerticalAlignment="Top" Width="130" IsEditable="True" SelectionChanged="comboBox_PC_Opt_SelectionChanged">
            <ComboBoxItem VerticalContentAlignment="Center">
                <StackPanel Orientation="Horizontal">
                    <Rectangle Fill="Blue" Width="15" Height="15" Margin="0,2,5,2" />
                    <TextBlock Text="Blue" />
                </StackPanel>
            </ComboBoxItem>
            <ComboBoxItem VerticalContentAlignment="Center">
                <StackPanel Orientation="Horizontal">
                    <Rectangle Fill="Black" Width="15" Height="15" Margin="0,2,5,2" />
                    <TextBlock Text="Black" />
                </StackPanel>
            </ComboBoxItem></ComboBox>

WPF组合框所选项目未显示其值

设置你的Combobox的DisplayMemberPath属性

一个简单的解决方案是使用TextSearch。TextPath附加属性。你可以试试下面的代码:

<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,330,557,0" 
          Name="comboBox_PC_Opt" VerticalAlignment="Top" Width="130" 
          IsEditable="True" 
          SelectionChanged="comboBox_PC_Opt_SelectionChanged"
          TextSearch.TextPath="Content.Children[1].Text"
>

注意,ComboBoxItem应该始终包含子元素(如在您的代码中)(以便Children[1]指向正确的TextBlock)。

UPDATE:如果你只是想在颜色名称旁边显示彩色矩形,而不需要使ComboBox可编辑,你可以删除IsEditable="True"

要获得选定的颜色,你必须使用SelectedValuePath指向内部RectangleFill.Color,当然这样ComboBoxItem应该总是包含StackPanel,这个面板应该包含Rectangle颜色作为第一项:

<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,330,557,0" 
          Name="comboBox_PC_Opt" VerticalAlignment="Top" Width="130" 
          IsEditable="True" 
          SelectionChanged="comboBox_PC_Opt_SelectionChanged"
          TextSearch.TextPath="Content.Children[1].Text"
          SelectedValuePath="Content.Children[0].Fill.Color"
>

然后在后面的代码中你可以像这样得到SelectedValue:

private void comboBox_PC_Opt_SelectionChanged(object sender, 
                                              SelectionChangedEventArgs e){
    var selectedValue = ((ComboBox)e.Source).SelectedValue;
    if(selectedValue != null){
        var selectedColor = (Color)selectedValue;
    }
}

当您可以简单地添加一个属性时,这是太多的代码。将tag属性添加到XAML:

中的comboboxitem中
<Comboboxitem Tag="Blue"/>

:

 GetValue=ComboboxName.SelectedItem.Tag.ToString()

GetValue将为"Blue"而不是"System.Windows.Controls.ComboBoxItem"

您正在获取控件的名称,因为组合框不知道从哪里获取所选值的文本。

相反,只需使用其他wpf控件,如headeredcontentcontrol,其中有一个header属性,一旦选择值将返回

示例:

      <Grid>
    <Grid.Resources>
        <ObjectDataProvider 
ObjectInstance="{x:Type Colors}" 
MethodName="GetProperties" 
x:Key="colorPropertiesOdp"  />
        <Style TargetType="{x:Type HeaderedContentControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <ContentPresenter ContentSource="Content" />
                            <ContentPresenter ContentSource="Header" Grid.Column="1" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <ComboBox ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" Height="23" HorizontalAlignment="Right" Name="comboBox_PC_Opt" VerticalAlignment="Top" Width="130" 
              >
        <ComboBox.ItemTemplate>
            <DataTemplate DataType="{x:Type Color}">
                <HeaderedContentControl Header="{Binding Path=Name}">
                    <Rectangle Fill="{Binding Path=Name}" Width="15" Height="15" Margin="0,2,5,2" />
                </HeaderedContentControl>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</Grid>