【发布时间】: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