带有集合的控件对值类型使用不正确的样式
Controls with a collection use the incorrect styling with value types
WPF 中带有集合的控件(例如 ListBox
或 ComboBox
)在使用值类型(例如 int
或 [=16] 时使用 TextBlock
样式而不是它们自己的样式=]).
可验证的例子:
Xaml代码:
<Window.Resources>
<Style TargetType="ComboBox">
<Setter Property="FontSize" Value="8"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Foreground" Value="Green" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="50"/>
<Setter Property="Foreground" Value="Red" />
</Style>
</Window.Resources>
<Grid>
<ComboBox Name="cbb1" Margin="0 -100 0 0"/>
<ComboBox Name="cbb2"/>
</Grid>
CS码:
//In the constructor
cbb1.ItemsSource = new[] { "A", "B", "C" };
cbb1.SelectedItem = cbb1.Items[0];
cbb2.ItemsSource = new[] { 1, 2, 3 };
cbb2.SelectedItem = cbb2.Items[0];
此示例代码将显示 2 个 ComboBox
,一个项目具有 'incorrect' TextBlock
样式,另一个具有 'normal' ComboBox
造型。
然后为所需的值类型制作 class(对我来说最常见的是 enum
)或删除样式是否有解决方法或修复方法?
使用不使用默认 TextBlock 样式的 TextBlock 声明 ItemTemplate:
<Style TargetType="ComboBox">
<Setter Property="FontSize" Value="8"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Foreground" Value="Green" />
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
WPF 中带有集合的控件(例如 ListBox
或 ComboBox
)在使用值类型(例如 int
或 [=16] 时使用 TextBlock
样式而不是它们自己的样式=]).
可验证的例子:
Xaml代码:
<Window.Resources>
<Style TargetType="ComboBox">
<Setter Property="FontSize" Value="8"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Foreground" Value="Green" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="50"/>
<Setter Property="Foreground" Value="Red" />
</Style>
</Window.Resources>
<Grid>
<ComboBox Name="cbb1" Margin="0 -100 0 0"/>
<ComboBox Name="cbb2"/>
</Grid>
CS码:
//In the constructor
cbb1.ItemsSource = new[] { "A", "B", "C" };
cbb1.SelectedItem = cbb1.Items[0];
cbb2.ItemsSource = new[] { 1, 2, 3 };
cbb2.SelectedItem = cbb2.Items[0];
此示例代码将显示 2 个 ComboBox
,一个项目具有 'incorrect' TextBlock
样式,另一个具有 'normal' ComboBox
造型。
然后为所需的值类型制作 class(对我来说最常见的是 enum
)或删除样式是否有解决方法或修复方法?
使用不使用默认 TextBlock 样式的 TextBlock 声明 ItemTemplate:
<Style TargetType="ComboBox">
<Setter Property="FontSize" Value="8"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Foreground" Value="Green" />
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>