更改 属性 模板中的控件定义

Changing property of control defines in template

我想访问在 MaterialDesignTheme.Clock.xaml

中定义的 MaterialDesignClockBackgroundEllipse

访问 Ellipse 后,我需要更改 BackgroundEllipse 的不透明度。

下面的示例代码不起作用。

<materialDesign:Clock x:Name="MaterialTimePicker" Is24Hours="True" 
                    BorderThickness="0" >
    <materialDesign:Clock.Style>
        <Style>
            <Setter TargetName="BackgroundEllipse" Property="Opacity" Value="1" />
        </Style>
    </materialDesign:Clock.Style>
</materialDesign:Clock>

如果不复制 整个 模板并编辑,则无法更改 XAML 中模板中包含的 EllipseOpacity它。 Opacity 的值被硬编码为 0.23.

不过您可以动态更改它:

private void MaterialTimePicker_Loaded(object sender, RoutedEventArgs e)
{
    Clock clock = (Clock)sender;
    Ellipse ellipse = clock.Template.FindName("BackgroundEllipse", clock) as Ellipse;
    if (ellipse != null)
    {
        ellipse.Opacity = 1.0;
    }
}

XAML:

<materialDesign:Clock x:Name="MaterialTimePicker" Is24Hours="True" BorderThickness="0"
    Loaded="MaterialTimePicker_Loaded"/>

这比 XAML 复制模板的解决方案需要更少的标记。