ComboBoxItem不显示

时间:2018-05-15 14:22:51

标签: wpf xaml

我在Popup中有一个ComboBox的样式。这里是 我的ComboBoxItem的Image  以下是代码:

d <- structure(list(string_column = c("apples grape banana satsuma", 
"grape banana satsuma melon"), c2 = c(3, 4)), row.names = c(NA, 
-2L), class = c("tbl_df", "tbl", "data.frame"))

ToggleButton和ComboBoxItems的样式:

<Style x:Key="ComboBoxStyle" TargetType="{x:Type ComboBox}">
        <Setter Property="IsSelected" Value="{DynamicResource ComboBoxItemStyle}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <Grid>
                        <ToggleButton Height="{TemplateBinding Height}"
                                      Style="{StaticResource ToggleButtonStyle}"  
                                      HorizontalAlignment="Right"
                                      Width ="549"
                                      IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                                      ClickMode="Press"/>
                        <Popup Name="Popup"
                               IsOpen="{TemplateBinding IsDropDownOpen}"
                               AllowsTransparency="True" 
                               Focusable="False"
                               PopupAnimation="Fade" 
                               VerticalOffset="10">
                            <Grid Name="DropDown"
                                  SnapsToDevicePixels="True"
                                  MinWidth="{TemplateBinding ActualWidth}"
                                  MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                <Border x:Name="DropDownBorder" CornerRadius="18"/>
                                <ScrollViewer SnapsToDevicePixels="True" VerticalScrollBarVisibility="Hidden">
                                    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                </ScrollViewer>
                            </Grid>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsGrouping"  Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                        </Trigger>
                        <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                            <Setter TargetName="DropDownBorder" Property="Background" Value="White"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

当我从列表中选择任何项目时,它不会显示。只是一个空格。我试图绑定SelectionBoxItem,但它不起作用。  如果我得到答案,我会很高兴。  感谢。

1 个答案:

答案 0 :(得分:0)

问题是您的模板不包含ContentPresenter,在哪里显示所选项目。它应该在这里:

<Style x:Key="ComboBoxStyle" TargetType="{x:Type ComboBox}">
        <Setter Property="ItemContainerStyle" Value="{StaticResource ComboBoxItemStyle}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <Grid>
                        <ToggleButton Height="{TemplateBinding Height}"
                                      Width="549"
                                  Style="{StaticResource ToggleButtonStyle}"  
                                  HorizontalAlignment="Right"
                                  IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                                  ClickMode="Press"/>
                        <ContentPresenter Name="ContentSite"
                                          HorizontalAlignment="Left"
                                          VerticalAlignment="Center"
                                          Content="{TemplateBinding ComboBox.SelectionBoxItem}" 
                                          ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}"/>
                        <Popup Name="Popup"...

另外,我不确定您的代码是否像在Combobox的样式设置器中那样有IsSelected属性。它有值,应该分配给ItemContainerStyle属性:

 <Setter Property="ItemContainerStyle" Value="{DynamicResource ComboBoxItemStyle}"/>

此外,我建议您不要使用DynamicResource,如果它对您没那么有价值。改为使用StaticResource。