【问题标题】:Dynamically change ellipse fill from c#从 c# 动态更改椭圆填充
【发布时间】:2016-06-19 07:57:09
【问题描述】:

我的“ButtonStyles.xaml”资源字典中有一个椭圆按钮的模板。 这是代码的一部分:

<SolidColorBrush x:Key="brush" Color="Red"/>
<Style TargetType="Button" x:Key="MyButtonStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <VisualState.Setters>
                                    <Setter Target="ellipse.(Shape.Fill)" Value="{StaticResource ResourceKey=brush}">
                                    </Setter>
                                    <Setter Target="ellipse.(Shape.Stroke)" Value="{x:Null}"/>
                                </VisualState.Setters>
                            </VisualState>
                            [...]
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Ellipse x:Name="ellipse"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是主页中的按钮:

<Button x:Name="toggle" Content="" HorizontalAlignment="Left" Height="132" Margin="40,146,0,0" VerticalAlignment="Top" Width="132" Style="{StaticResource MyButtonStyle}"/>

现在我想从 c# 代码更改画笔资源。为此,我尝试如下:

Application.Current.Resources["brush"] = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 125, 126, 126));

虽然画笔资源有效地改变了椭圆的颜色却没有。我认为问题在于我不应该更改模板中的颜色,而是更改实际 MainPage 按钮中的颜色。 这是问题吗?我该怎么做?

【问题讨论】:

  • 如果您想在代码中动态更改填充,为什么要将其设为静态资源?为什么不将其绑定到字段?
  • 这会以某种方式帮助你 Ellipse Fill Color with c#

标签: c# xaml win-universal-app fill ellipse


【解决方案1】:

我根据@felix-castor 的建议和this 指南得到了结果。我在 C# 代码中放置了一个 DependencyProperty,在 XAML 代码中放置了一个绑定。

它工作得很好,但是当属性更改时它不会刷新按钮,我必须在 VisualStates 之间进行更改以使其更新填充。我会发布另一个关于它的问题。但是,如果您有任何建议,您可以发表评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    相关资源
    最近更新 更多