以下是我的ComboBox样式代码。思路是在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>