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


编辑:

终于解决了这个问题,仍然想知道如何用最少的 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>
4

1 回答 1

4

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>
于 2013-05-18T18:42:13.690 回答