我正在制作一个简单的textBox模板,它只有两个边框,一个带有渐变背景。现在,我的具体问题是我希望能够将textBox的前景色设置为我想要的任何颜色并使其正常工作。但是,我似乎无法同时使残疾前景色和启用的前景色一起工作。例如,如果我将前景设置为红色,当我禁用textBox时,前景不会更改为我的禁用颜色。我尝试在IsEnabled =“true”触发器中绑定前景,但这似乎不起作用。无论textBox是否启用,前景始终保持红色。
你能看一下下面的模板,告诉我我做错了什么吗?此外,请指出我自从我创建模板以来所犯的任何其他错误。
非常感谢。
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
<SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />
<SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />
<Style x:Key="TextBoxControlTemplate1" TargetType="{x:Type TextBox}">
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="Background" Value="#00000000"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="FontFamily" Value="Segoe UI"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Padding" Value="8,5,3,3"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Border BorderBrush="#FF000000" BorderThickness="2,2,2,2" CornerRadius="5,5,5,5" Padding="0,0,0,0" Width="Auto" Height="Auto" Background="#FF000000"/>
<Border x:Name="Border" BorderBrush="#FFFFFFFF" BorderThickness="1,1,1,1" CornerRadius="5,5,5,5" Padding="0,0,0,0" Width="Auto" Height="Auto" Margin="2,2,2,2">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000" Offset="0"/>
<GradientStop Color="#FF4D4D4D" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/>
<Setter Property="BorderBrush" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/>
<Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Foreground" Value="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<TextBox Text="TEST" TextWrapping="Wrap" Canvas.Top="293.761" Canvas.Left="112" Style="{DynamicResource TextBoxControlTemplate1}" Height="28.724" Width="232.25" IsTabStop="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" IsEnabled="True" Foreground="#FFFF0000"/>
答案 0 :(得分:3)
这里有一些不同的问题都是针对你的。第一个是您在控件实例上设置特定的Foreground值,该值的优先级高于Trigger
中Style
为控件本身属性设置的值。这与在ControlTemplate
内的元素上设置的属性不同,如“Border”。您使用Trigger
设置Border
属性的方式就是说明这一点。通常,您还希望使用TemplateBinding
s将控件实例上设置的值作为默认值(如Background
)提取,这些值当前被忽略。
要在样式控件上切换属性的两个值,就像您想要使用Foreground
一样,您可以在Setter
中使用Trigger
和Style
提供默认值和备用值。这仍然可以被实例上设置的值覆盖。如果您要禁止覆盖Trigger
的实例,请将其设置为“边框”Trigger
,其中您要在ControlTemplate
内的元素上设置值。
我建议的最后一项更改是切换到您正在拉入Style
的画笔的StaticResource。在这种情况下,它可能不会有所作为,但在某些情况下,默认样式可以被拉入一个上下文,该上下文没有从声明它的文件中引用周围的资源。使用Static将保证它将包括那些资源,无论它在何处被使用。您可能不会遇到这种情况,但在设置这样的样式/模板时,这是一个很好的习惯。
以下是具有这些改进的代码:
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
<Style x:Key="TextBoxControlTemplate1" TargetType="{x:Type TextBox}">
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000" Offset="0"/>
<GradientStop Color="#FF4D4D4D" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="FontFamily" Value="Segoe UI"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Padding" Value="8,5,3,3"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="BorderBrush" Value="#FF000000"/>
<Setter Property="Foreground" Value="Red" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<!--Take advantage of containment when possible to let the layout engine help you!-->
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5" Padding="0" Background="#FF000000"/>
<Border x:Name="Border" BorderBrush="#FFFFFFFF" BorderThickness="1" CornerRadius="5" Padding="0" Margin="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"/>
<ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{StaticResource DisabledBackgroundBrush}" TargetName="Border"/>
<Setter Property="BorderBrush" Value="{StaticResource DisabledBackgroundBrush}" TargetName="Border"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
而TextBox
仅使用Style
设置:
<TextBox Text="TEST" TextWrapping="Wrap" Canvas.Top="293.761" Canvas.Left="112" Style="{DynamicResource TextBoxControlTemplate1}"
Height="28.724" Width="232.25" IsTabStop="False" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" />
答案 1 :(得分:1)
尝试一些想法:
摆脱你的一个触发器。有两个相反的触发器可能不是一个好主意。我会直接在您的Background
声明中设置默认BorderBrush
,Foreground
和Border
,然后移除Enabled="True"
触发器。然后,调试只是让Enabled="False"
触发器正确。
将TargetName
属性添加到Enabled="False"
触发器的setter中。
这是一个远景,但使用UIElement.IsEnabled
而不是IsEnabled
,如下所示:<Trigger Property="UIElement.IsEnabled">
。
希望我在这里说的话有所帮助!
答案 2 :(得分:1)
可能的原因是您在使用自己的风格时无法看到DisabledBackgroundBrush
。请尝试将样式添加到ControlTemplate
的资源中:
<ControlTemplate TargetType="{x:Type TextBox}">
<ControlTemplate.Resources>
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
...
</ControlTemplate.Resources>
...
顺便说一句,您的控件模板不会尊重您的属性值。 例如,您应该使用类似
的内容<ScrollViewer Margin="{TemplateBinding Padding}" x:Name="PART_ContentHost"/>
在你的控制模板中。