ComboBoxItem не отображается - PullRequest
0 голосов
/ 15 мая 2018

У меня есть стиль для ComboBox во всплывающем окне.Вот Изображение моего ComboBoxItem Вот код:

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

Стили для ToggleButton и ComboBoxItems:

<Style x:Key="ToggleButtonStyle" TargetType="{x:Type ToggleButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="520"/>
                        </Grid.ColumnDefinitions>
                        <Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="10,10,10,10" Background="White" />
                        <Path Data="M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M7,10L12,15L17,10H7Z" 
                              Fill="#FF16CD1C" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Column="1" Margin="0,0,10,0"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="#E1E1E1" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


 <Style x:Key="ComboBoxItemStyle" TargetType="{x:Type ComboBoxItem}">
        <Setter Property="Background" Value="White"/>
        <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}, Path=IsInDesignMode}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                    <Border Background="{TemplateBinding Background}"
                            CornerRadius="18"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <ContentPresenter HorizontalAlignment="Center"/>
                        <Border.Triggers>
                            <EventTrigger RoutedEvent="MouseEnter">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetProperty="(ComboBoxItem.Background).(SolidColorBrush.Color)"
                                        FillBehavior="HoldEnd"
                                        To="#E1E1E1"
                                        Duration="0:0:0.4"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                            <EventTrigger RoutedEvent="MouseLeave">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetProperty="(ComboBoxItem.Background).(SolidColorBrush.Color)"
                                                        FillBehavior="HoldEnd"
                                                        To="#FFFFFF"
                                                        Duration="0:0:0.4"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </Border.Triggers>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Когда я выбираю любой элемент из списка, он не отображается. Просто пробел.Я пытался привязать SelectionBoxItem, но он не работает.Я буду так счастлив, если получу ответ.Спасибо.

1 Ответ

0 голосов
/ 15 мая 2018

Проблема в том, что ваш шаблон не содержит 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"...

Кроме того, я не совсем уверен, что ваш код работает, так как в Style Setter Combobox есть свойство IsSelected.И это имеет значение, которое должно быть присвоено свойству ItemContainerStyle:

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

Кроме того, я бы порекомендовал вам не использовать DynamicResource, если он не столь полезен для вас.Вместо этого используйте StaticResource.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...