小编bgc*_*ode的帖子

WPF 样式不适用于已设置样式的 UserControl

我来找你是因为我对控件样式感到头疼了几个小时。通过给用户控件定义样式是行不通的!

我的用户控件声明:

<uiComponent:NumericTextBox Text="{Binding myProperty}"/>
Run Code Online (Sandbox Code Playgroud)

我想要应用的样式:

<Style TargetType="uiComponent:NumericTextBox">
   <Setter Property="Background" Value="Black"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

为什么它不能与背景属性一起使用,尽管它可以与可见性属性一起使用!我尝试使用 TargetType=FrameworkElement,没有效果......

我的用户控件是一个数字文本框,它定义了自己的样式,如下所示:

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
  xmlns:l="clr-namespace:LSX.Space.PropertyUI.NumericTextBox">

    <SolidColorBrush x:Key="CustomTextBox_Background" Color="White" />
    <SolidColorBrush x:Key="CustomTextBox_Foreground" Color="Black" />
    <LinearGradientBrush x:Key="CustomTextBox_Border" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Color="#FFABADB3" Offset="0.05" />
        <GradientStop Color="#FFE2E3EA" Offset="0.07" />
        <GradientStop Color="#FFE3E9EF" Offset="1" />
    </LinearGradientBrush>

    <Style x:Key="{x:Type l:NumericTextBox}" TargetType="{x:Type l:NumericTextBox}">
        <Setter Property="Background" Value="{StaticResource CustomTextBox_Background}" />
        <Setter Property="BorderBrush" Value="{StaticResource CustomTextBox_Border}" />
        <Setter Property="Foreground" Value="{StaticResource CustomTextBox_Foreground}" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate …
Run Code Online (Sandbox Code Playgroud)

wpf

5
推荐指数
1
解决办法
7771
查看次数

标签 统计

wpf ×1