C# 如何自定义“中ContentControl内的属性”;按「;VisualState?
我正在构建Windows Phone应用程序,我创建了一个自定义图标按钮,可以反复使用。该按钮与下图非常相似: 为了尝试遵循DRY(不要重复自己)原则,我创建了一个基本上继承自C# 如何自定义“中ContentControl内的属性”;按「;VisualState?,c#,wpf,silverlight,windows-phone-7,windows-phone-8,C#,Wpf,Silverlight,Windows Phone 7,Windows Phone 8,我正在构建Windows Phone应用程序,我创建了一个自定义图标按钮,可以反复使用。该按钮与下图非常相似: 为了尝试遵循DRY(不要重复自己)原则,我创建了一个基本上继承自按钮的类,并添加了一个名为IconTemplate的ControlTemplate类型的自定义参数 为了简洁起见,我省略了文本中有关属性的部分 创建类之后,我创建了一个名为Generic.xaml的资源,它对这个类应用了一些样式 <ResourceDictionary xmlns="http://schemas.m
按钮的类,并添加了一个名为IconTemplate
的ControlTemplate
类型的自定义参数
为了简洁起见,我省略了文本
中有关属性的部分
创建类之后,我创建了一个名为Generic.xaml的资源
,它对这个类应用了一些样式
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:PiadasEngracadas.Controls"
<Style TargetType="c:IconButton">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
<Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="c:IconButton">
<Grid Background="{TemplateBinding Background}" x:Name="ButtonBackground">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="IconTemplateContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="IconTemplateContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
<DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentControl x:Name="IconTemplateContainer"
Template="{TemplateBinding IconTemplate}" />
<TextBlock VerticalAlignment="Bottom"
HorizontalAlignment="Right"
Margin="{StaticResource PhoneHorizontalMargin}"
FontFamily="{StaticResource PhoneFontFamilyLight}"
Text="{TemplateBinding Text}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
但问题是我不知道如何更改路径的颜色,该路径是ContentControl
的模板(?)。我在想这样的事情:
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Stroke" Storyboard.TargetName="(IconTemplateContainer).(Template).(Stroke)">
可以对IconButton的前景色路径的笔划颜色进行模板绑定。然后,您可以更改IconButton的前景色,它将更改路径颜色:Stroke=“{TemplateBinding Foreground}”如何覆盖自定义按钮中的MouseDown
事件,并从此处更改路径
笔划?听起来您可以将路径的笔划颜色绑定到图标按钮的前景色。然后,您可以更改图标按钮的前景色,它将更改路径颜色:Stroke=“{TemplateBinding Foreground}”
如果您可以发布包含路径的ContentTemplate代码,这将非常有用。问题是我不知道如何访问路径的笔划颜色,因为它位于我的资源文件中的ContentControl
中TemplateBinding正是为了这个目的:从模板内部绑定属性将属性添加到正在使用它们的外部控件上。我可能误解了你。你能为我澄清一下吗?这确实对我有用。我基本上是将属性SolidBrush IconButton.TapStrokeBrush
添加到我的类中,然后在ControlTemplate
中添加Stroke=“{TemplateBinding Foreground}”
和泛型。xaml的ContentControl
我在VisualState中进行了绑定
。按下了
<c:IconButton Text="Some Text"
IconTemplate="{StaticResource IconsButtons.Sleepy}" />
<c:IconButton Text="Some Text"
TappedColor="#123456" // New property
IconTemplate="{StaticResource IconsButtons.Sleepy}" />
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Stroke" Storyboard.TargetName="(IconTemplateContainer).(Template).(Stroke)">
<ControlTemplate x:Key="IconsButtons.Sleepy">
<Path Data="M32.000099,44.658999C36.566562,44.658999 39.162999,47.058804 ...."
Stretch="Uniform"
Fill="#FFFFFFFF"
Width="26"
Height="26"
Margin="0"
RenderTransformOrigin="0.5,0.5">
<Path.RenderTransform>
<TransformGroup>
<TransformGroup.Children>
<RotateTransform Angle="0" />
<ScaleTransform ScaleX="1" ScaleY="1" />
</TransformGroup.Children>
</TransformGroup>
</Path.RenderTransform>
</Path>
</ControlTemplate>