UWP : How to avoid style name(x:Name) in Templete Control Development?

Viewed 224

With UWP ( or WPF ), I add x:Name on my XAML-style. and Style does not work at all with x:name. Why ?

Now, I am developing a Template control. To change style property with "SetValue", I have to add name on my Style. then I added name x:Name="MyStyleName" but It make problem.

Here is a simple example code with Style x:Name.

    <Grid Background="Black" Width="200" Height="200" >

        <Grid.Resources>
            <Style x:Name="MyStyleName"  TargetType="Ellipse">
                <Setter Property="Height" Value="100" />
                <Setter Property="Width" Value="100" />
                <Setter Property="Stroke" Value="Red"/>
            </Style>
        </Grid.Resources>

        <Ellipse x:Name="Circle1" />
        <Ellipse x:Name="Circle2" />

    </Grid>

enter image description here

Main reason that I add x:Name on style, During Template control development, I want to access STYLE by name.

        var _grid = this.GetTemplateChild(GRID_NAME) as Grid;
        if (_grid != null)
        {
            // change grid margin.
            _grid.Margin = new Thickness(0, 5, 0, 0);

            // get Style of Grid
            var _gridStyle = _grid .Resources[GRID_STYLE_NAME] as Style;
            if(_gridStyle != null)
            {
                // Change Gird Height as STYLE !
                _gridStyle.SetValue(FrameworkElement.HeightProperty, 10);

            }
        }

Is there any good way to avoid Style x:Name ??

1 Answers
Related