积极答复者
控件模板IsEnabled

问题
答案
-
修改控件模板里的属性触发器
比如
<ControlTemplate.Triggers> <Trigger Property="UIElement.IsEnabled"> <Setter Property="TextElement.Foreground"> <Setter.Value> <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" /> </Setter.Value> </Setter> <Trigger.Value> <s:Boolean>False</s:Boolean> </Trigger.Value> </Trigger> </ControlTemplate.Triggers> </ControlTemplate>
Hero- 已标记为答案 仁虎 2011年12月20日 15:10
全部回复
-
修改控件模板里的属性触发器
比如
<ControlTemplate.Triggers> <Trigger Property="UIElement.IsEnabled"> <Setter Property="TextElement.Foreground"> <Setter.Value> <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" /> </Setter.Value> </Setter> <Trigger.Value> <s:Boolean>False</s:Boolean> </Trigger.Value> </Trigger> </ControlTemplate.Triggers> </ControlTemplate>
Hero- 已标记为答案 仁虎 2011年12月20日 15:10
-
举例: 在Window窗口中添加WPF的TextBox控件,然后把这个TextBox控件的IsEnabled属性设置成False,这时看到这个控件的颜色是灰白色的.我是想把这个控件在'IsEnabled=False'这个状态下的灰白色更改成无色或其他颜色.
那么,是不是只有通过修改其控件的Style样式或ControlTemplate模板来做呢.
1.从Style入手,对背景色调整后,界面中控件在'IsEnabled=False'这个状态下依旧显示是灰白色,无效
再添加触发器'IsEnabled=False'状态,将背景色调整,控件显示依旧无效
2.从ControlTemplate模板入手,将Bd与PART_ContentHost背景色调整,控件显示依旧无效
再从触发器'IsEnabled=False'状态调整(这个状态是生成模板时已建好的),将背景色调整,控件显示依旧无效
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
x:Class="MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480"><Window.Resources>
<ControlTemplate x:Key="TextBoxControlTemplate1" TargetType="{x:Type TextBox}">
<Microsoft_Windows_Themes:ListBoxChrome x:Name="Bd">
<ScrollViewer x:Name="PART_ContentHost"/>
</Microsoft_Windows_Themes:ListBoxChrome>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{x:Null}"/>
<Setter Property="Background" TargetName="Bd" Value="{x:Null}"/>
<Setter Property="Background" TargetName="PART_ContentHost" Value="{x:Null}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="TextBoxStyle1" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{x:Null}"/>
</Trigger>
</Style.Triggers>
<Setter Property="Background" Value="{x:Null}"/>
</Style>
</Window.Resources><Grid x:Name="LayoutRoot">
<TextBox Height="34" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Center" IsEnabled="False" HorizontalAlignment="Center" Style="{DynamicResource TextBoxStyle1}"/>
</Grid>
</Window>