当我直接从属性信息更改依赖属性时,在我运行应用程序之前,我的设计视图中没有立即得到结果。
<local:OnOffToggleButton HorizontalAlignment="Left" Margin="35,34,0,0" x:Name="onOffToggleButton1" VerticalAlignment="Top" StateOf4="3" GotFocus="onOffToggleButton1_GotFocus" Loaded="onOffToggleButton1_Loaded" />
public class OnOffToggleButton : Control// ToggleButton
{
public static readonly DependencyProperty StateOf4Property;
static OnOffToggleButton()
{
OnOffToggleButton.StateOf4Property = DependencyProperty.Register("StateOf4", typeof(int), typeof(OnOffToggleButton));
}
public int StateOf4
{
get
{
return (int)this.GetValue(OnOffToggleButton.StateOf4Property);
}
set
{
SetValue(OnOffToggleButton.StateOf4Property, value);
}
}
}
}
这是我的切换按钮的默认值、模板和样式
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3">
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="buttonTemplate" TargetType="{x:Type local:OnOffToggleButton}" >
<StackPanel>
<Grid>
<Ellipse x:Name="btnEllipse" StrokeThickness="2" Stroke="DarkBlue" Width="128" Height="131">
<Ellipse.Fill>
<RadialGradientBrush Center="0.3,0.2" RadiusX="0.5" RadiusY="0.5">
<GradientStop Color="Azure" Offset="0.1" />
<GradientStop Color="Red" Offset="1.1" />
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
</Grid>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="StateOf4" Value="1">
<Setter TargetName="btnEllipse" Property="Fill">
<Setter.Value>
<RadialGradientBrush Center="0.3,0.2" RadiusX="0.5" RadiusY="0.5">
<GradientStop Color="Green" Offset="0.1" />
<GradientStop Color="GreenYellow" Offset="1.1" />
</RadialGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="StateOf4" Value="2">
<Setter TargetName="btnEllipse" Property="Fill">
<Setter.Value>
<RadialGradientBrush Center="0.3,0.2" RadiusX="0.5" RadiusY="0.5">
<GradientStop Color="BlueViolet" Offset="0.1" />
<GradientStop Color="Pink" Offset="1.1" />
</RadialGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="StateOf4" Value="3">
<Setter TargetName="btnEllipse" Property="Fill">
<Setter.Value>
<RadialGradientBrush Center="0.3,0.2" RadiusX="0.5" RadiusY="0.5">
<GradientStop Color="Maroon" Offset="0.1" />
<GradientStop Color="Pink" Offset="1.1" />
</RadialGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style TargetType="{x:Type local:OnOffToggleButton}">
<Setter Property="Control.Template" Value="{StaticResource buttonTemplate}" />
</Style>
你能帮我吗!:)