小编TFF*_*FFR的帖子

WPF SubControl(如TextBlock)不使用TemplateSelector从窗口继承Style

我需要帮助,因为我不明白为什么来自datatemplate的控件不会继承窗口资源中定义的样式.可能有一个解决方法吗?

如果有人能给我一个解决方案,我会非常感激,因为我花了很多时间找到一些东西.

特此我的例子.例如,horrizontal模板中的Texblock不对齐:

Udapte:我添加了背景颜色.样式应用于标签,但不应用于由datatemplate定义的totextblock和textbox.

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:localview="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <Style x:Key="{x:Type TextBlock}" TargetType="TextBlock" >
            <Setter Property="Background" Value="Cyan"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="3"/>
            <Setter Property="FontFamily" Value="Comic Sans MS"/>
        </Style>
        <Style x:Key="{x:Type Label}" TargetType="Label">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
        <Style x:Key="{x:Type TextBox}" TargetType="TextBox">
            <Setter Property="Background" Value="Cyan"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="3"/>
        </Style>
        <Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="3"/>
        </Style>

        <localview:TemplateSelector …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml

4
推荐指数
2
解决办法
1113
查看次数

标签 统计

c# ×1

wpf ×1

xaml ×1