【发布时间】:2016-03-06 11:28:04
【问题描述】:
所以,我在资源字典中有一个按钮模板。它在其正常 VisualState 中的颜色属性绑定到 MainPage C# 代码中定义的 DependencyProperty。请注意 MainControl 是 MainPage 名称。 这是 Button 模板:
<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="{Binding ElementName=MainControl, Path=ButtonColor}">
</Setter>
<Setter Target="ellipse.(Shape.Stroke)" Value="{x:Null}"/>
</VisualState.Setters>
</VisualState>
[Other Visual States...]
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Ellipse x:Name="ellipse"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这是 MainPage XAML:
<Page
x:Class="Volumio.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Volumio"
Name="MainControl">
<Grid Background="#FF525C66">
<Button x:Name="toggle" Content="" HorizontalAlignment="Left" Height="132" Margin="40,146,0,0" VerticalAlignment="Top" Width="132" Style="{StaticResource MyButtonStyle}">
</Grid>
这里是我定义属性的地方:
public static DependencyProperty ColorProperty = DependencyProperty.Register("ButtonColor", typeof(SolidColorBrush),
typeof(Page), PropertyMetadata.Create(NormalColor));
public SolidColorBrush ButtonColor
{
get { return (SolidColorBrush)GetValue(ColorProperty); }
set { SetValue(ColorProperty, value); }
}
问题是:每当我将 ButtonColour 属性设置为不同的颜色时,Button 不会自动更改其颜色,而是我必须更改 VisualState 然后恢复正常以使其刷新并更改颜色。我更改了异步空隙中的属性。这有关系吗?
问题出在哪里?我必须从代码中手动刷新它吗?
这可能很明显,但我是 XAML 编程的新手。提前致谢。
【问题讨论】:
标签: c# xaml binding win-universal-app