wpf - 如何访问模板控件内的控件属性?
问题描述
我想更改模板控件内的控件属性。例如前景。但是当我改变时,注意到发生了。我在下面的代码中将前景设置为 Aqua,在将 IsEnabled 设置为 false 后什么也没发生(只是不透明度发生了变化)
<CheckBox Name="chbx" Content="Check Box" Foreground="White"
HorizontalAlignment="Center" IsEnabled="False" >
<CheckBox.Template>
<ControlTemplate TargetType="CheckBox">
<BulletDecorator>
<BulletDecorator.Bullet>
<Border Width="15" Height="15" x:Name="border"
Background="Transparent" BorderBrush="LightGray"
BorderThickness="1" ClipToBounds="True">
<Border.Effect>
<DropShadowEffect BlurRadius="5" ShadowDepth="2"></DropShadowEffect>
</Border.Effect>
<Path x:Name="CheckMark"
Width="8"
Height="8"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 0 L 8 8 M 0 8 L 8 0"
Stretch="Fill"
Stroke="LightGray"
StrokeEndLineCap="Round"
StrokeStartLineCap="Round"
StrokeThickness="2"
/>
</Border>
</BulletDecorator.Bullet>
<ContentPresenter Margin="04,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
RecognizesAccessKey="True"
/>
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" Value="White"></Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.2"/>
<Setter Property="Foreground" Value="Aqua"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</CheckBox.Template>
</CheckBox>
解决方案
推荐阅读
- c++ - 在一个 QFileSystemModel 上应用两个 FileFilterProxyModel
- node.js - 角度项目 npm 模块随机删除
- java - 从相机拍摄多张图像并将每个字节数组转换为 base64
- python - 无法使用 Selenium 从网页中找到文本
- python - 如何使用 Python(也许还有 pandas?)呈现表格数据
- python - 检查目标时出错:预期 dense_1 有 2 个维度,但得到了形状为 (20, 84, 1) 的数组
- android - 如何在扩展浮动操作按钮单击android上实现菜单显示
- python - AttributeError:“SMOTE”对象没有属性“_validate_data”
- typescript - Firebase Firestore 将地图项添加到数组字段
- c++ - C++ 初学者:为什么我的编译器会根据我的循环返回“未找到名称”?