【问题标题】:How do I display the selected item and use borders with a combobox? And why does this behavior occur?如何显示所选项目并使用带有组合框的边框?为什么会发生这种行为?
【发布时间】:2011-08-04 15:24:28
【问题描述】:

如何使ComboBox 的选定项目显示在ComboBox 文本字段中并在ComboBox ControlTemplate 中使用Borders?使用以下代码,项目弹出很好,但在选择后从未出现在 ComboBox 文本字段中;但是从 ComboBox 模板中删除 2 Borders 可以解决此问题。为什么??如何??更重要的是:我如何将此模板与Borders 一起使用,并在选择后让SelectedItem 正确显示在ComboBox 文本字段中?

<Window.Resources>
    <Style x:Key="ComboboxDropdownButton" TargetType="{x:Type ToggleButton}">
        <Setter Property="MinWidth" Value="0"/>
        <Setter Property="MinHeight" Value="0"/>
        <Setter Property="Width" Value="NaN"/>
        <Setter Property="Height" Value="NaN"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <DockPanel SnapsToDevicePixels="True" 
                               Background="{TemplateBinding Background}" 
                               LastChildFill="False">
                        <Border x:Name="Border" 
                                Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" 
                                DockPanel.Dock="Right" 
                                Background="WhiteSmoke" 
                                CornerRadius="0,3,3,0"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                >
                            <Path Fill="{TemplateBinding Foreground}"
                                  HorizontalAlignment="Center" VerticalAlignment="Center" Data="M0,0L4.5,4 9,0z"/>
                        </Border>
                    </DockPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="Border" Property="Background" Value="White" />
                        </Trigger>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter TargetName="Border" Property="Background" Value="White" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value="0.5"/>
            </Trigger>
        </Style.Triggers>
    </Style>

    <ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}">
        <Border x:Name="PART_ContentHost" Focusable="False" />
    </ControlTemplate>

    <Style x:Key="{x:Type ComboBox}"
           TargetType="{x:Type ComboBox}">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="true" />
        <Setter Property="MinWidth" Value="120" />
        <Setter Property="MinHeight" Value="20" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <Border SnapsToDevicePixels="True" 
                            x:Name="OuterBorder" 
                            Background="Transparent" 
                            BorderBrush="Red" 
                            BorderThickness="1" 
                            CornerRadius="4"
                            Margin="-1">
                        <Border x:Name="InnerBorder" 
                                Background="WhiteSmoke" 
                                BorderThickness="1" 
                                CornerRadius="3" 
                                BorderBrush="Black">
                            <Grid>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="EditStates">
                                        <VisualState x:Name="Editable">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                                               Storyboard.TargetName="PART_EditableTextBox">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                                               Storyboard.TargetName="ContentSite">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Uneditable" />
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <ToggleButton x:Name="ToggleButton"
                                              Margin="-1"
                                              Grid.Column="2"
                                              Focusable="False"
                                              ClickMode="Press"
                                              Style="{StaticResource ComboboxDropdownButton}"
                                              IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
                                </ToggleButton>
                                <ContentPresenter x:Name="ContentSite"
                                                  IsHitTestVisible="False"
                                                  Content="{TemplateBinding SelectionBoxItem}"
                                                  ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                                  ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                                  Margin="3,3,23,3"
                                                  VerticalAlignment="Stretch"
                                                  HorizontalAlignment="Left">
                                </ContentPresenter>
                                <TextBox x:Name="PART_EditableTextBox"
                                         Style="{x:Null}"
                                         Template="{StaticResource ComboBoxTextBox}"
                                         HorizontalAlignment="Left"
                                         VerticalAlignment="Bottom"
                                         Margin="3,3,23,3"
                                         Focusable="True"
                                         Background="Transparent"
                                         Visibility="Hidden"
                                         IsReadOnly="{TemplateBinding IsReadOnly}" />
                                <Popup x:Name="PART_Popup"
                                       Placement="Bottom"
                                       IsOpen="{TemplateBinding IsDropDownOpen}"
                                       AllowsTransparency="True"
                                       Focusable="False"
                                       PopupAnimation="Slide">
                                    <Grid x:Name="DropDown"
                                          SnapsToDevicePixels="True"
                                          MinWidth="{TemplateBinding ActualWidth}"
                                          MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                          >
                                        <Border x:Name="DropDownBorder"
                                                MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                                MinWidth="{Binding ActualWidth, ElementName=Border}" 
                                                Background="WhiteSmoke" 
                                                BorderBrush="Black" 
                                                BorderThickness="1" CornerRadius="0,0,3,3">
                                        </Border>
                                        <ScrollViewer Padding="1" SnapsToDevicePixels="True">
                                            <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                        </ScrollViewer>
                                    </Grid>
                                </Popup>
                            </Grid>
                        </Border>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasItems" Value="false">
                            <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95" />
                        </Trigger>
                        <Trigger Property="IsGrouping" Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Grid>
    <ComboBox Width="120" Height="20" Name="comboBox1" SnapsToDevicePixels="True"
              HorizontalAlignment="Center" VerticalAlignment="Center"
              Text="ComboBox" SelectedIndex="0" IsEditable="True" IsReadOnly="True">
        <ComboBoxItem>item 1</ComboBoxItem>
        <ComboBoxItem>item 2</ComboBoxItem>
        <ComboBoxItem>item 3</ComboBoxItem>
    </ComboBox>
</Grid>

【问题讨论】:

  • 只是一个完全疯狂的猜测......几个月前我们遇到了同样的情况,当时我们的自定义组合框模板中没有间歇性地显示组合框弹出和选择,当我们删除 @987654333 时它消失了@来自边境的财产.... :-/ ...我知道这听起来很愚蠢而且完全荒谬,但这就是实际发生的事情!这是一个我迄今为止无法解决的谜团......如果这个“绝望”的伎俩不起作用,你可以投票给我...... :-D
  • 同样奇怪的事情曾经发生在我身上。也很奇怪:在 .NET 3.0 中,我们在 TextBlock 上添加了 DropShadowBitmap 效果。它导致了内存泄漏,导致我们的应用程序以大约 1 MB/分钟的速度增加。
  • 是的!那个也是!我们的启动画面有圆角和 DropShadowBitmap 和一个组合框,它的行为完全荒谬!当我们删除这两个时,它开始正常工作!
  • 不,圆角半径不是问题,即使我在各处移除圆角半径并仅添加 2 个基本边框而不设置它们的任何属性,这仍然会发生。仅当我完全删除 2 个边框时才会显示所选项目,但随后我失去了组合框所需的外观。

标签: wpf xaml combobox wpf-controls selecteditem


【解决方案1】:

我终于解决了这个问题.. 通过从模板中删除边框。我用模板网格中的矩形替换了边框。像魅力一样工作。结果可以在下一个问题here中找到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多