UWP根据类按钮扩展中的ButtonType属性更改按钮样式的最佳方法是什么



我正试图用这样的ButtonType来扩展按钮类:

public class MyButton : Button 
{
public MyButton()
{
}
public string ButtonType
{
get { return (string)GetValue(ButtonTypeProperty); }
set
{SetValue(ButtonTypeProperty, value);}
}
// Using a DependencyProperty as the backing store for ButtonType.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty ButtonTypeProperty =
DependencyProperty.Register("ButtonType", typeof(string), typeof(MyButton), new PropertyMetadata(0));
}

我的按钮模板:

<ControlTemplate TargetType="controls:RMButton">
<Grid x:Name="BtnInnerGrid" RenderTransformOrigin="0.5,0.5" BorderBrush="{ThemeResource RMWhiteSmoke}" BorderThickness="2" Background="{ThemeResource RMTranslucentBlackBrush}" CornerRadius="20" Padding="0">
<Grid.RenderTransform>
<CompositeTransform ScaleX="1" ScaleY="1" />
</Grid.RenderTransform>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="00:00:00.200000">
<VisualTransition.GeneratedEasingFunction>
<CubicEase EasingMode="EaseOut" />
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal">
<Storyboard>
<PointerUpThemeAnimation Storyboard.TargetName="BtnInnerGrid" />
</Storyboard>
</VisualState>
<VisualState x:Name="PointerOver">
<VisualState.Setters>
<Setter Target="BtnInnerGrid.(UIElement.Opacity)" Value="0.7" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Target="BtnInnerGrid.(UIElement.Opacity)" Value="0.7" />
<Setter Target="BtnInnerGrid.(UIElement.RenderTransform).(CompositeTransform.ScaleX)" Value="0.9" />
<Setter Target="BtnInnerGrid.(UIElement.RenderTransform).(CompositeTransform.ScaleY)" Value="0.9" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Target="BtnInnerGrid.(UIElement.Opacity)" Value="0.3" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.RowDefinitions>
<RowDefinition x:Name="BtnGridTopRow" />
<RowDefinition x:Name="BtnGridBottomRow" />
</Grid.RowDefinitions>
<RelativePanel x:Name="TopRowRelativePanel" Grid.Column="0" Grid.Row="0">
<Viewbox x:Name="FontIconViewBox" Visibility="Collapsed">
<FontIcon x:Name="BtnFontIcon" FontFamily="Segoe MDL2 Assets" Glyph="&#xE700;" />
</Viewbox>
<Viewbox x:Name="ImageViewBox" Visibility="Visible">
<Image x:Name="BtnImage" Source="" />
</Viewbox>
</RelativePanel>
<RelativePanel x:Name="BottomRowRelativePanel" Grid.Column="0" Grid.Row="1">
<TextBlock x:Name="BtnTextBlock"  Text="Test fdgfxgsg sdg dsgdf sgdf" />
</RelativePanel>
</Grid>
</ControlTemplate>

例如,对于某个类型,我需要BtnGridTopRow.Height = 2*;Button.Width=150;,对于另一个类型,则需要BtnGridTopRow.Height = 0;Button.Width=100;。目标是每个按钮都有相同的CommonStates,但有各种按钮样式,但继承了BorderBrush、背景等的主样式。

我正在寻找最好的方法来做到这一点,因为我是XAML和UWP的新手,但我在web(PHP、JS、CSS等(方面有15年的经验

希望它足够清楚,

提前感谢

UWP根据类按钮扩展中的ButtonType属性更改按钮样式的最佳方法是什么?

更好的方法是使VisualState控制不同的按钮类型样式。并在CCD_ 6性质发生变化时进入不同的视觉状态。

例如

public enum XButtonType
{
TopState,
HiddenTopState
}
public sealed class MyButton : Button
{
public MyButton()
{
this.DefaultStyleKey = typeof(MyButton);
}
public XButtonType ButtonType
{
get { return (XButtonType)GetValue(ButtonTypeProperty); }
set { SetValue(ButtonTypeProperty, value); }
}
// Using a DependencyProperty as the backing store for ButtonType.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty ButtonTypeProperty =
DependencyProperty.Register("ButtonType", typeof(XButtonType), typeof(MyButton), new PropertyMetadata(0, new PropertyChangedCallback(OnButtonTypeChange)));
private static void OnButtonTypeChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as MyButton;
switch ((XButtonType)e.NewValue)
{
case XButtonType.TopState:
VisualStateManager.GoToState(control, "TopState", true);
break;
case XButtonType.HiddenTopState:
VisualStateManager.GoToState(control, "HiddenTopState", true);
break;
default:
break;
}
}
}

Xaml代码

<Style TargetType="local:MyButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyButton">
<Grid
x:Name="BtnInnerGrid"
Padding="0"
BorderThickness="2"
CornerRadius="20"
RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<CompositeTransform ScaleX="1" ScaleY="1" />
</Grid.RenderTransform>
<Grid.RowDefinitions>
<RowDefinition x:Name="BtnGridTopRow" />
<RowDefinition x:Name="BtnGridBottomRow" />
</Grid.RowDefinitions>
<RelativePanel
x:Name="TopRowRelativePanel"
Grid.Row="0"
Grid.Column="0">
<Viewbox x:Name="FontIconViewBox" Visibility="Collapsed">
<FontIcon
x:Name="BtnFontIcon"
FontFamily="Segoe MDL2 Assets"
Glyph="&#xE700;" />
</Viewbox>
<Viewbox x:Name="ImageViewBox" Visibility="Visible">
<Image x:Name="BtnImage" Source="" />
</Viewbox>
</RelativePanel>
<RelativePanel
x:Name="BottomRowRelativePanel"
Grid.Row="1"
Grid.Column="0">
<TextBlock x:Name="BtnTextBlock" Text="Test this is button content!" />
</RelativePanel>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="00:00:00.200000">
<VisualTransition.GeneratedEasingFunction>
<CubicEase EasingMode="EaseOut" />
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal">
<Storyboard>
<PointerUpThemeAnimation Storyboard.TargetName="BtnInnerGrid" />
</Storyboard>
</VisualState>
<VisualState x:Name="PointerOver">
<VisualState.Setters>
<Setter Target="BtnInnerGrid.(UIElement.Opacity)" Value="0.7" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Target="BtnInnerGrid.(UIElement.Opacity)" Value="0.7" />
<Setter Target="BtnInnerGrid.(UIElement.RenderTransform).(CompositeTransform.ScaleX)" Value="0.9" />
<Setter Target="BtnInnerGrid.(UIElement.RenderTransform).(CompositeTransform.ScaleY)" Value="0.9" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Target="BtnInnerGrid.(UIElement.Opacity)" Value="0.3" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="ButtonTypeGroup">
<VisualState x:Name="TopState">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BtnGridTopRow" Storyboard.TargetProperty="Height">
<DiscreteObjectKeyFrame KeyTime="0" Value="2*" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BtnInnerGrid" Storyboard.TargetProperty="Width">
<DiscreteObjectKeyFrame KeyTime="0" Value="150" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
<!--<VisualState.Setters>
<Setter Property="Width" Value="100"/>
</VisualState.Setters>-->
</VisualState>
<VisualState x:Name="HiddenTopState">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BtnGridTopRow" Storyboard.TargetProperty="Height">
<DiscreteObjectKeyFrame KeyTime="0" Value="0" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BtnInnerGrid" Storyboard.TargetProperty="Width">
<DiscreteObjectKeyFrame KeyTime="0" Value="100" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

用法

<Grid>
<local:MyButton  VerticalAlignment="Center" ButtonType="TopState"/>
</Grid>

最新更新