Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有集合的控件对值类型使用了不正确的样式_C#_Wpf_Value Type_Itemssource_Reference Type - Fatal编程技术网

C# 带有集合的控件对值类型使用了不正确的样式

C# 带有集合的控件对值类型使用了不正确的样式,c#,wpf,value-type,itemssource,reference-type,C#,Wpf,Value Type,Itemssource,Reference Type,在WPF中具有集合的控件(例如列表框或组合框)在使用值类型(如int或enum)时使用TextBlock样式,而不是自己的样式 可验证示例: Xaml代码: 此示例代码将显示2个组合框,其中一个项目具有“不正确的”文本块样式,另一个项目具有“正常的”组合框样式 除了为所需的值类型创建一个类(我最常用的是enums)或删除样式之外,是否有解决方法或修复方法?使用TextBlock声明一个ItemTemplate,它不使用默认TextBlock样式: <Style TargetType="C

在WPF中具有集合的控件(例如
列表框
组合框
)在使用值类型(如
int
enum
)时使用
TextBlock
样式,而不是自己的样式

可验证示例:

Xaml代码:


此示例代码将显示2个
组合框
,其中一个项目具有“不正确的”
文本块
样式,另一个项目具有“正常的”
组合框
样式


除了为所需的值类型创建一个类(我最常用的是
enum
s)或删除样式之外,是否有解决方法或修复方法?

使用TextBlock声明一个ItemTemplate,它不使用默认TextBlock样式:

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

声明一个带有TextBlock的ItemTemplate,该TextBlock不使用默认的TextBlock样式:

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


有趣又奇怪。你知道为什么它会区分值类型和引用类型(在样式方面)吗?对不起,不知道。您可以在.Net参考源代码中检查ContentPresenter的实现。有趣且奇怪。你知道为什么它会区分值类型和引用类型(在样式方面)吗?对不起,不知道。您可以在.Net引用源中检查ContentPresenter的实现。
<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>