动态更改 C# 中的椭圆填充
本文关键字:填充 动态 | 更新日期: 2023-09-27 18:37:27
我的"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));
虽然画笔资源有效地改变了椭圆的颜色,但不会。我认为问题是我不应该更改模板中的颜色,而应该更改实际的主页按钮中的颜色。这是问题所在吗?我该怎么做?
我按照@felix-castor的建议和本指南得到了一个结果。我在 C# 代码中放置了一个依赖项属性,在 XAML 代码中放置了一个绑定。
它运行良好,但是当属性更改时它不会刷新按钮,我必须在 VisualStates 之间切换才能使其更新填充。我会发布另一个关于它的问题。但是,如果您有建议,可以发表评论。