简体   繁体   中英

Changing property of control defines in template

I want to access BackgroundEllipse of MaterialDesignClock which defines in MaterialDesignTheme.Clock.xaml

After accessing the Ellipse , I need to change the opacity of BackgroundEllipse .

The sample code below did not work.

<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>

You cannot change the Opacity of the Ellipse that is included in the template in XAML without copying the entire template and edit it. The value of the Opacity is hardcoded to 0.23 .

You may change it dynamically though:

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"/>

This requires a lot less markup than the XAML solution of copying the template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM