【发布时间】:2013-08-11 08:16:17
【问题描述】:
我来找你是因为我有几个小时对控件样式感到头疼。 通过为用户控件定义样式,它不起作用!
我的用户控件声明:
<uiComponent:NumericTextBox Text="{Binding myProperty}"/>
我要应用的样式:
<Style TargetType="uiComponent:NumericTextBox">
<Setter Property="Background" Value="Black"/>
</Style>
为什么它不能与 Background 属性一起使用,尽管它可以与 Visibility 属性一起使用! 我用 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 TargetType="{x:Type l:NumericTextBox}">
<Border x:Name="Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid x:Name="LayoutGrid">
<ScrollViewer Margin="2" x:Name="PART_ContentHost" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<!--Message validation des erreurs-->
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="HasText" Value="True" />
<Condition Property="Validation.HasError" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path= TextError}"/>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<Image x:Name="ValidationIcon" DockPanel.Dock="Left" Stretch="None" Width="15" Height="15" Source="pack://application:,,,/LS.Net.Telcom.Space.PropertyUI;component/Images/validationError.png" />
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder />
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
非常感谢您的帮助。
【问题讨论】:
-
由于您设置的是
UserControl的ControlTemplate而不是直接的内容,我相信您可能需要修改Binding以向上移动模板...Background="{TemplateBinding Background, RelativeSource={RelativeSource TemplatedParent}}" -
对不起,它不起作用。
-
这个怎么样...
Background="{TemplateBinding Background, RelativeSource={RelativeSource AncestorType={x:Type l:NumericTextBox}}}" -
uiComponent:NumericTextBox 是否派生自 TextBox ?
-
是的,它来自文本框
标签: wpf