WPF下拉框样式设计

3
以下是我的ComboBox样式代码。思路是在ComboBox周围放置一个边框并重用该样式。
<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>

然而,在应用此样式后,组合框中的选项未显示出来。
<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>

为什么ComboBox项目未显示?
编辑:

最终选择了这个,但还是想知道如何用最少的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>
1个回答

5

ComboBox 有几个命名部分,负责为不同部分进行样式设置。这意味着模板的某些部分必须正确命名以便在正确的位置使用。我认为你想要为 ContentPresenterBorder 设置样式。所有信息已在此MSDN站点上解释清楚。

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>

1
谢谢您的回复,现在我明白了如何为组合框设置样式。组合框有5个部分。在创建样式时,需要处理ContentPresenter、ToggleButton和Popup,以便样式可以生效。 - Abbas
抱歉没有表达清楚,示例只是整个MSDN模板的一小部分,只是一个粗略的想法,以向您展示代码中的问题。编写ComboBox的完整工作模板可能需要更多的工作。 - dkozl
2
是的,我确实同意你的观点。组合框的样式需要更多的工作。除此之外,它还要求在处理样式XAML方面具备专业实践经验。对于初学者来说,组合框不是一个容易进行样式设计的控件! - Abbas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接