簡體   English   中英

WPF ComboBox SelectedItem未顯示其值

[英]WPF ComboBox SelectedItem is not showing its value

我已經創建了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>

所以,我該如何解決..

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 )。

更新 :如果只想在顏色名稱旁邊顯示彩色矩形而無需使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;
    }
}

當您只需添加一個屬性時,這就是太多代碼。 將標記屬性添加到XAML中的comboboxitem中:

<Comboboxitem Tag="Blue"/>

然后:

 GetValue=ComboboxName.SelectedItem.Tag.ToString()

GetValue將是“藍色”,而不是“ 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM