[英]UWP XAML and C# - x:Bind on a DataTemplate inside a ResourceDictionary
[英]UWP - How to convert a ResourceDictionary XAML Style to C#
我正在尝试创建XAML样式的C#版本,但是我很难添加ContentPresenter/ContentTemplate
和VisualStateManagers
。 当然,这应该不难,但事实证明确实如此。
例如,我将如何创建它的C#版本。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button">
<Setter Property="Background" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" />
<Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
<Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundTransparentBrush}" />
<Setter Property="BorderThickness" Value="0,0,0,2" />
<Setter Property="Padding" Value="12,8,12,8" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="FontSize" Value="{ThemeResource TextStyleLargeFontSize}" />
<Setter Property="UseSystemFocusVisuals" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="RootGrid"
Background="{TemplateBinding Background}">
<Border x:Name="FocusOuterRectangle"
Margin="-3,-3,0,0"
BorderBrush="{ThemeResource SystemControlForegroundTransparentBrush}"
BorderThickness="3,3,3,1">
<ContentPresenter x:Name="ContentPresenter"
Padding="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTransitions="{TemplateBinding ContentTransitions}"
FontSize="{TemplateBinding FontSize}"
TextWrapping="WrapWholeWords" />
</Border>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource myButtonBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource myBlackBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource myWhiteBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="FocusOuterRectangle"
Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource SystemControlForegroundTransparentBrush}" />
</ObjectAnimationUsingKeyFrames>
<PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
</Storyboard>
</VisualState>
<VisualState x:Name="PointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource myButtonHoverBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource myBlackBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource myWhiteBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="FocusOuterRectangle"
Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource SystemControlForegroundTransparentBrush}" />
</ObjectAnimationUsingKeyFrames>
<PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
到目前为止,我已经知道了:
var style = new Style();
style.TargetType = typeof(Button);
style.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.MyButtonBrush));
style.Setters.Add(new Setter(Button.ForegroundProperty, Brushes.MyWhiteBrush));
style.Setters.Add(new Setter(Button.BorderBrushProperty, Brushes.MyBlackBrush));
style.Setters.Add(new Setter(Button.BorderThicknessProperty, new Thickness(0, 0, 0, 2)));
style.Setters.Add(new Setter(Button.PaddingProperty, new Thickness(12, 8, 12, 8)));
style.Setters.Add(new Setter(Button.HorizontalAlignmentProperty, HorizontalAlignment.Left));
style.Setters.Add(new Setter(Button.VerticalAlignmentProperty, VerticalAlignment.Center));
style.Setters.Add(new Setter(Button.FontFamilyProperty, FontFamily.XamlAutoFontFamily));
style.Setters.Add(new Setter(Button.FontWeightProperty, FontWeights.Normal));
style.Setters.Add(new Setter(Button.FontSizeProperty, TextStyleLargeFontSize));
style.Setters.Add(new Setter(Button.UseSystemFocusVisualsProperty, false));
var controlTemplate = new ControlTemplate { TargetType = typeof(Button) };
var contentPresenter = new ContentPresenter
{
Name = "ContentPresenter",
Padding = new Thickness(0, 0, 0, 2),
HorizontalContentAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center,
BorderBrush = Brushes.MyBlackBrush,
BorderThickness = new Thickness(0, 0, 0, 2),
FontSize = TextStyleLargeFontSize,
TextWrapping = TextWrapping.WrapWholeWords
};
但是我不确定如何将ContentPresenter
添加到ControlTemplate
或创建包含ContentPresenter
的Grid
。 然后我不确定如何开始创建VisualStateGroups
等。
非常感谢您的帮助。
我认为不可能在后面的代码中编写等效的代码。 在WPF中, ControlTemplate
具有VisualTree
属性,该属性允许在代码隐藏中进行设置,并且您还可以使用FrameworkElementFactory
创建FrameworkTemplate
子类,例如ControlTemplate
或DataTemplate
,但是这些选项不完整并且不能提供所有XAML灵活性。 而是可以使用XamlReader
类在运行时从string
中解析XAML代码。 这样,您也许可以在后台代码中创建它,但是我看不出有太多理由仅使用已有的XAML代码来这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.