WPF ComboBox SelectedItem未显示其值

时间:2014-07-10 10:44:42

标签: c# wpf xaml combobox

我已经创建了WPF应用程序。我有一个颜色的ComboBox。我希望选择的颜色作为我的ComboBoxItem,但当我从ComboBox中选择一个项目时,它会显示'System.Windows.Controls.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>

那么,我该如何解决这个问题呢?

4 个答案:

答案 0 :(得分:1)

DisplayMemberPath属性设置为Combobox。

答案 1 :(得分:0)

一个简单的解决方案是使用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)。< / p>

更新:如果您只想在颜色名称旁边显示彩色矩形而无需使用ComboBox进行编辑,则可以删除IsEditable="True"

要获取所选颜色,您必须使用SelectedValuePath指向内部Fill.Color的{​​{1}},当然这样Rectangle应始终包含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

答案 2 :(得分:0)

当您只需添加一个属性时,代码太多了。将标记属性添加到XAML中的组合框:

</head>
<body class="mdl-layout mdl-js-layout">
    <!--Main Page -->
    <main class="mdl-layout__content page-content">
        <!--Content Section One -->
        <section class="section-one mdl-grid">

然后:

</head>
<body>
    <div class="mdl-layout mdl-js-layout">
        <!--Main Page -->
        <main class="mdl-layout__content page-content">
            <!--Content Section One-->
            <section class="section-one mdl-grid">

GetValue将是“Blue”而不是“System.Windows.Controls.ComboBoxItem”

答案 3 :(得分:-1)

你得到了控件的名称,因为组合框不知道从哪里获得 所选值的文本。

相反,只需使用其他wpf控件(如headeredcontentcontrol),而不是在选择值后将返回的头属性

示例:

      <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>
相关问题