【问题标题】:Windows 7 style ComboBox in WPFWPF 中的 Windows 7 样式组合框
【发布时间】:2011-04-01 02:17:21
【问题描述】:

我正在尝试在 WPF 中重新创建您在 Windows 资源管理器中看到的 ComboBox 样式。我正在尝试创建类似于“排列方式:”组合框的东西,例如,它位于“库”->“文档”下。在您将鼠标悬停在组合框上之前,组合框没有轮廓,单击时会显示一个允许单个选择的上下文菜单。

【问题讨论】:

    标签: wpf combobox styles


    【解决方案1】:

    您可以覆盖默认模板。使用触发器根据组合框是否具有焦点来指定要使用的模板。

    例如:

    <Style TargetType="{x:Type ComboBox}">
        <Setter Property="Template" Value="{StaticResource TemplateWhenFocused}" />
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver" Value="False" />
                    <Condition Property="IsFocused" Value="False" />
                </MultiTrigger.Conditions>
                <Setter Property="Template" Value="{StaticResource TemplateWhenNotFocused}" />
            </MultiTrigger>
        </Style.Triggers>
    </Style>
    

    示例非焦点模板。在打开或关闭对象时,可能需要调整边距以避免文本“跳跃”。

    <ControlTemplate TargetType="{x:Type ComboBox}" x:Key="StyleWhenNotFocused">
        <TextBlock Text="{TemplateBinding Text}"
                   Foreground="{TemplateBinding Foreground}"
                   Background="{TemplateBinding Background}"
                   Padding="{TemplateBinding Padding}"
                   Margin="2,0,2,0" />
    </ControlTemplate>
    

    【讨论】:

      猜你喜欢
      • 2012-04-17
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多