首页 > 解决方案 > 如何访问模板控件内的控件属性?

问题描述

我想更改模板控件内的控件属性。例如前景。但是当我改变时,注意到发生了。我在下面的代码中将前景设置为 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>

标签: wpf

解决方案


推荐阅读