WPF组合框样式 [英] WPF Combobox Styling

查看:182
本文介绍了WPF组合框样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的组合框样式代码。

Below is my combobox style code. Idea is to put a border around the combobox and reuse the style.

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="Window1.xaml">
  <Application.Resources>
    <Style x:Key="UserInputComboBoxStyle"
           TargetType="{x:Type ComboBox}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ComboBox}">
            <Grid>
              <Border BorderBrush="Black"
                      BorderThickness="2"
                      VerticalAlignment="Stretch"
                      HorizontalAlignment="Stretch" />
              <ComboBox HorizontalAlignment="Stretch"
                        VerticalAlignment="Center"
                        HorizontalContentAlignment="Left"
                        VerticalContentAlignment="Center"
                        Margin="5">
              </ComboBox>
            </Grid>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Application.Resources>
</Application>

但是,在应用此样式后,在生成的组合框中不会显示组合框项目。

However after applying this style, in the resultant combobox the combobox items are not getting displayed.

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ComboBoxTest"
        Height="300"
        Width="300">
  <StackPanel>
    <ComboBox Margin="5"
              Style="{StaticResource UserInputComboBoxStyle}">
      <ComboBoxItem Content="Test0"
                    IsSelected="True" />
      <ComboBoxItem Content="Test1" />
      <ComboBoxItem Content="Test2" />
    </ComboBox>
  </StackPanel>
</Window>

任何人都可以帮我让这项工作吗?

Can anyone please help me to get this working?

种类:
阿巴斯

Kind Regards, Abbas

================== ==================================================== ===

============================================================================

最后结算,仍然想知道如何使用最少数量的xaml代码。

<Grid>
  <Border BorderBrush="Black"
          BorderThickness="2"
          CornerRadius="5">
    <ComboBox SelectedIndex="0"
              VerticalAlignment="Top"
              HorizontalAlignment="Stretch"
              VerticalContentAlignment="Center"
              HorizontalContentAlignment="Center"
              BorderBrush="Black"
              BorderThickness="5"
              Margin="5">
      <ComboBoxItem IsSelected="True">Test0</ComboBoxItem>
      <ComboBoxItem>Test1</ComboBoxItem>
      <ComboBoxItem>Test2</ComboBoxItem>
      <ComboBoxItem>Test3</ComboBoxItem>
    </ComboBox>
  </Border>
</Grid>


推荐答案

ComboBox 有很少的命名部分负责定义不同的部分,这意味着模板的某些部分必须正确命名,以便在正确的地方使用。我想你想风格 ContentPresenterBorder 。在此 MSDN 中解释了一切。网站。

ComboBox has few named parts responsible for styling different parts which means certain parts of template must be named properly in order to be used in correct places. I think you want to style ContentPresenterBorder. Everything has been explained on this MSDN site.

MSDN示例:

<Setter Property="Template">
   <Setter.Value>
      <ControlTemplate TargetType="ComboBox">
         <Grid>
            <Border x:Name="ContentPresenterBorder">
               <Grid>
                  <ToggleButton x:Name="DropDownToggle"/>
                  <ContentPresenter x:Name="ContentPresenter" />
                     <TextBlock Text=" " />
                  </ContentPresenter>
               </Grid>
            </Border>
         </Grid>
      </ControlTemplate>
   </Setter.Value>
</Setter>

这篇关于WPF组合框样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆