ComboBox does not show selected item

时间:2015-09-14 16:06:38

标签: wpf

I do not get selected item displayed in ComboBox ever since I have overwritten the default style with this one:

 <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="Blue" Offset="0.0"/>
                    <GradientStop Color="White" Offset="1.0"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <SolidColorBrush x:Key="WindowBackgroundBrush" Color="White" />


        <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="20" />
                </Grid.ColumnDefinitions>
                <Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="1" Background="#5089ba" BorderThickness="1" BorderBrush="White"/>
                <Border Grid.Column="0" CornerRadius="1"  Margin="1"  Background="{StaticResource WindowBackgroundBrush}" BorderThickness="0,0,1,0" />
                <Path x:Name="Arrow" Grid.Column="1" HorizontalAlignment="Center" Fill="White"  VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>
            </Grid>
        </ControlTemplate>


        <Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBox">
                        <Grid>
                            <ToggleButton  Name="ToggleButton" Template="{StaticResource ComboBoxToggleButton}" Grid.Column="2" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press">
                            </ToggleButton>
                            <ContentPresenter Name="ContentSite" IsHitTestVisible="True"   VerticalAlignment="Center" HorizontalAlignment="Center" />

                            <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
                                <Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                    <Border x:Name="DropDownBorder" Background="{StaticResource WindowBackgroundBrush}" BorderThickness="1"/>
                                    <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                    </ScrollViewer>
                                </Grid>
                            </Popup>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
            </Style.Triggers>
        </Style>

Selection still works fine and does what it is supposed to do. It is just that I don't get the selected item displayed in header. What could be the reason?

3 个答案:

答案 0 :(得分:1)

这解决了这个问题:

<ContentPresenter Name="ContentSite" IsHitTestVisible="True"  VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding SelectionBoxItem}"/>

我错过了内容模板绑定。

答案 1 :(得分:1)

 <ContentPresenter Name="ContentSite" IsHitTestVisible="True"   VerticalAlignment="Center" HorizontalAlignment="Center"                                                    Content="{TemplateBinding ComboBox.SelectionBoxItem}"
ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}"/>

现在它会起作用;)

答案 2 :(得分:0)

IT's in your Content presenter.

You need to add a textblock.

<ContentPresenter Name="ContentSite" IsHitTestVisible="True"   VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock Text=" " />  
</ContentPresenter >