下面是我的 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>