设置边框背景与模板结合 [英] Setting Border background with a template binding
问题描述
VALUE ={TemplateBinding HeaderColor}我已经创建了自己的控制,我想知道,如果我能在Border.Background绑定到一个模板属性。我目前正在与类似下面的静态资源设置它:
<颜色X:键=ControlMouseOverColor>绿色< /颜色>< ColorAnimationUsingKeyFrames Storyboard.TargetProperty=\"(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)\" Storyboard.TargetName =headerLayout>
&所述; EasingColorKeyFrame KeyTime =0:0:6值={StaticResource的ControlMouseOverColor}/>
< / ColorAnimationUsingKeyFrames>
我想这是对我的控制属性,并能够将其作为一个模板结合
< ColorAnimationUsingKeyFrames Storyboard.TargetProperty=\"(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)\" Storyboard.TargetName =headerLayout>
&所述; EasingColorKeyFrame KeyTime =0:0:6值={TemplateBinding HeaderColor}/>
< / ColorAnimationUsingKeyFrames>
MainPage.xaml中
<按Ctrl:选择Grid.Column =0HeaderColor =红的HeaderText =头文字/>
我的类:
公共静态只读的DependencyProperty HeaderColorProperty =
DependencyProperty.Register(HeaderColor的typeof(System.Windows.Media.Color)的typeof(精选),新PropertyMetadata(System.Windows.Media.Colors.Red));公共System.Windows.Media.Color HeaderColor {
{返回(System.Windows.Media.Color)的GetValue(HeaderColorProperty); }
集合{的SetValue(HeaderColorProperty,值); }
}
这第二个选项是不行的,我应该能够做到这一点?我没有得到一个错误,它只是不会更改为我设置的颜色。
由AngelWPF留下注释要求更多code,粘贴下面,我在学习创建一个控件的开始阶段,要注意的是,因为有很多我还没有到没有一一块在一个时间:)
generic.xaml
< ResourceDictionary中
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:控制=CLR的命名空间:SelectionControl.Library
的xmlns:CTRL =CLR的命名空间:SelectionControl.Library;装配= SelectionControl><一个LinearGradientBrush X:键=HeaderBackground终点=0.5,1StartPoint可以=0.5,0>
<渐变停止颜色=黑偏移量=0/>
<渐变停止颜色=灰色偏移量=1/>
< /一个LinearGradientBrush><颜色X:键=ControlMouseEnterColor>艾莉斯蓝< /颜色>
<颜色X:键=ControlMouseLeaveColor>灰色< /颜色>
<颜色X:键=ControlLeftMouseUpColor>红色和LT; /颜色><风格的TargetType =CTRL:选择>
< setter属性=宽度值=自动/>
< setter属性=高度值=自动/>
< setter属性=字号VALUE =12/>
< setter属性=粗细VALUE =大胆/>
< setter属性=前景VALUE =艾莉斯蓝/>
< setter属性=保证金VALUE =2,2,2,2/> < setter属性=背景VALUE ={StaticResource的的ResourceKey = HeaderBackground}/> < setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType =CTRL:选择>
<电网X:NAME =rootElement的保证金={TemplateBinding保证金}>
<! - 可视状态 - >
< VisualStateManager.VisualStateGroups>
< VisualStateGroup X:NAME =CommonStates>
<的VisualState X:名称=的MouseEnter>
<情节提要>
< ColorAnimationUsingKeyFrames Storyboard.TargetProperty=\"(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)\" Storyboard.TargetName =headerLayout>
< EasingColorKeyFrame KeyTime =0:0:5VALUE ={TemplateBinding HeaderColor}/>
< / ColorAnimationUsingKeyFrames>
< /故事板>
< /&的VisualState GT;
<的VisualState X:NAME =鼠标离开>
<情节提要>
< ColorAnimationUsingKeyFrames Storyboard.TargetProperty=\"(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)\" Storyboard.TargetName =headerLayout>
< EasingColorKeyFrame KeyTime =0:0:1VALUE ={StaticResource的ControlMouseLeaveColor}/>
< / ColorAnimationUsingKeyFrames>
< /故事板>
< /&的VisualState GT;
<的VisualState X:NAME =MouseLeftUp>
<情节提要>
< ColorAnimationUsingKeyFrames Storyboard.TargetProperty=\"(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)\" Storyboard.TargetName =headerLayout>
< EasingColorKeyFrame KeyTime =0:0:1VALUE ={StaticResource的ControlLeftMouseUpColor}/>
< / ColorAnimationUsingKeyFrames>
< /故事板>
< /&的VisualState GT;
< / VisualStateGroup>
< /VisualStateManager.VisualStateGroups>
<! - 结束可视状态 - > < Grid.RowDefinitions>
< RowDefinition高度=自动/>
< RowDefinition高度=*/>
< /Grid.RowDefinitions>
<! - 标题 - >
< BORDER X:NAME =headerLayout背景={TemplateBinding背景}Grid.Row =0的HorizontalAlignment =拉伸VerticalAlignment =拉伸CornerRadius =2,2,2,2BorderBrush =黑了borderThickness =1>
<&StackPanel的GT;
<&切换按钮GT;< /切换按钮>
< TextBlock的前景={TemplateBinding前景}文本={TemplateBinding的HeaderText}粗细={TemplateBinding粗细}字号={TemplateBinding字号}的HorizontalAlignment =中心VerticalAlignment =中心/>
< / StackPanel的>
< /边框>
<! - 正文内容 - >
<内容presenter Grid.Row =1CONTENT ={TemplateBinding内容}的HorizontalAlignment =拉伸VerticalAlignment =拉伸/>
< /网格和GT;
< /控件模板>
< /Setter.Value>
< /二传手>
< /样式和GT;
< / ResourceDictionary的>
Selection.cs
使用System.Windows;
使用System.Windows.Controls的;
使用System.Windows.Input;命名空间SelectionControl.Library {
[TemplateVisualState(名称= Selection.MouseEnterStateName,组名= Selection.CommonStatesGroupName)
[TemplateVisualState(名称= Selection.MouseLeaveStateName,组名= Selection.CommonStatesGroupName)
[TemplateVisualState(名称= Selection.MouseLeftUpStateName,组名= Selection.CommonStatesGroupName)公共类选择:ContentControl中{
公共常量字符串CommonStatesGroupName =CommonStates;
公共常量字符串MouseEnterStateName =的MouseEnter;
公共常量字符串MouseLeaveStateName =鼠标离开;
公共常量字符串MouseLeftUpStateName =MouseLeftUp; 公共选择(){
this.DefaultStyleKey = typeof运算(精选); this.MouseEnter + =新MouseEventHandler(OnMouseEnter在);
this.MouseLeave + =新MouseEventHandler(OnMouseLeave在);
this.MouseLeftButtonUp + =新MouseButtonEventHandler(OnMouseLeftButtonUp);
} 无效OnMouseLeftButtonUp(对象发件人,MouseButtonEventArgs E){
this.GoToState(Selection.MouseLeftUpStateName,真);
} 无效OnMouseLeave在(对象发件人,MouseEventArgs E){
this.GoToState(Selection.MouseLeaveStateName,真);
} 无效OnMouseEnter在(对象发件人,MouseEventArgs E){
this.GoToState(Selection.MouseEnterStateName,真);
}
私人无效GotoState函数(字符串Statename的,布尔useTransitions){
VisualStateManager.GoToState(这一点,Statename的,useTransitions);
} 公共静态只读的DependencyProperty HeaderTextProperty =
DependencyProperty.Register(的HeaderText的typeof(串)的typeof(精选),新PropertyMetadata()); 公共字符串的HeaderText {
{返回(串)的GetValue(HeaderTextProperty); }
集合{的SetValue(HeaderTextProperty,值); }
} 公共静态只读的DependencyProperty HeaderColorProperty =
DependencyProperty.Register(HeaderColor的typeof(System.Windows.Media.Color)的typeof(精选),新PropertyMetadata(System.Windows.Media.Colors.Red)); 公共System.Windows.Media.Color HeaderColor {
{返回(System.Windows.Media.Color)的GetValue(HeaderColorProperty); }
集合{的SetValue(HeaderColorProperty,值); }
}
}}
我已经对自定义依赖属性使用TemplateBinding结果好坏参半。正因为如此,我使用的RelativeSource TemplatedParent这似乎在任何情况下工作。
< EasingColorKeyFrame KeyTime =0:0:5VALUE ={结合HeaderColor,的RelativeSource = {的RelativeSource TemplatedParent}}/>
Value="{TemplateBinding HeaderColor}"I've created my own control, and I'm wondering if I can bind a Border.Background to a template property. Currently i'm setting it with a StaticResource like the following:
<Color x:Key="ControlMouseOverColor">green</Color>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout">
<EasingColorKeyFrame KeyTime="0:0:6" Value="{StaticResource ControlMouseOverColor}" />
</ColorAnimationUsingKeyFrames>
I'd like it be a property on my control, and be able to set it as a template binding
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout">
<EasingColorKeyFrame KeyTime="0:0:6" Value="{TemplateBinding HeaderColor}" />
</ColorAnimationUsingKeyFrames>
MainPage.xaml
<ctrl:Selection Grid.Column="0" HeaderColor="Red" HeaderText="Header Text" />
my class:
public static readonly DependencyProperty HeaderColorProperty =
DependencyProperty.Register("HeaderColor", typeof(System.Windows.Media.Color), typeof(Selection), new PropertyMetadata(System.Windows.Media.Colors.Red));
public System.Windows.Media.Color HeaderColor {
get { return (System.Windows.Media.Color)GetValue(HeaderColorProperty); }
set { SetValue(HeaderColorProperty, value); }
}
This 2nd option does not work, should I be able to do this? I don't get an error, it just does not change to the color I set.
Comment left by AngelWPF asked for more code, pasting below, I'm in the beginning stages of learning to create a control, wanted to note that, because there is a lot I have not got to yet, one piece at a time :)
generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:SelectionControl.Library"
xmlns:ctrl="clr-namespace:SelectionControl.Library;assembly=SelectionControl">
<LinearGradientBrush x:Key="HeaderBackground" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="Gray" Offset="1" />
</LinearGradientBrush>
<Color x:Key="ControlMouseEnterColor">aliceblue</Color>
<Color x:Key="ControlMouseLeaveColor">Gray</Color>
<Color x:Key="ControlLeftMouseUpColor">Red</Color>
<Style TargetType="ctrl:Selection">
<Setter Property="Width" Value="Auto" />
<Setter Property="Height" Value="Auto" />
<Setter Property="FontSize" Value="12" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="AliceBlue" />
<Setter Property="Margin" Value="2,2,2,2" />
<Setter Property="Background" Value="{StaticResource ResourceKey=HeaderBackground}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ctrl:Selection">
<Grid x:Name="RootElement" Margin="{TemplateBinding Margin}">
<!-- Visual States -->
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="MouseEnter">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout">
<EasingColorKeyFrame KeyTime="0:0:.5" Value="{TemplateBinding HeaderColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseLeave">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout">
<EasingColorKeyFrame KeyTime="0:0:1" Value="{StaticResource ControlMouseLeaveColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseLeftUp">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout">
<EasingColorKeyFrame KeyTime="0:0:1" Value="{StaticResource ControlLeftMouseUpColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<!-- End Visual States-->
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- Header -->
<Border x:Name="headerLayout" Background="{TemplateBinding Background}" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="2,2,2,2" BorderBrush="Black" BorderThickness="1">
<StackPanel>
<ToggleButton ></ToggleButton>
<TextBlock Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding HeaderText}" FontWeight="{TemplateBinding FontWeight}" FontSize="{TemplateBinding FontSize}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
</Border>
<!-- Body Content -->
<ContentPresenter Grid.Row="1" Content="{TemplateBinding Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Selection.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace SelectionControl.Library {
[TemplateVisualState(Name = Selection.MouseEnterStateName, GroupName = Selection.CommonStatesGroupName)]
[TemplateVisualState(Name = Selection.MouseLeaveStateName, GroupName = Selection.CommonStatesGroupName)]
[TemplateVisualState(Name = Selection.MouseLeftUpStateName, GroupName = Selection.CommonStatesGroupName)]
public class Selection : ContentControl {
public const string CommonStatesGroupName = "CommonStates";
public const string MouseEnterStateName = "MouseEnter";
public const string MouseLeaveStateName = "MouseLeave";
public const string MouseLeftUpStateName = "MouseLeftUp";
public Selection() {
this.DefaultStyleKey = typeof(Selection);
this.MouseEnter += new MouseEventHandler(OnMouseEnter);
this.MouseLeave += new MouseEventHandler(OnMouseLeave);
this.MouseLeftButtonUp += new MouseButtonEventHandler(OnMouseLeftButtonUp);
}
void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
this.GoToState(Selection.MouseLeftUpStateName, true);
}
void OnMouseLeave(object sender, MouseEventArgs e) {
this.GoToState(Selection.MouseLeaveStateName, true);
}
void OnMouseEnter(object sender, MouseEventArgs e) {
this.GoToState(Selection.MouseEnterStateName, true);
}
private void GoToState(string stateName, bool useTransitions) {
VisualStateManager.GoToState(this, stateName, useTransitions);
}
public static readonly DependencyProperty HeaderTextProperty =
DependencyProperty.Register("HeaderText", typeof(string), typeof(Selection), new PropertyMetadata(""));
public string HeaderText {
get { return (string)GetValue(HeaderTextProperty); }
set { SetValue(HeaderTextProperty, value); }
}
public static readonly DependencyProperty HeaderColorProperty =
DependencyProperty.Register("HeaderColor", typeof(System.Windows.Media.Color), typeof(Selection), new PropertyMetadata(System.Windows.Media.Colors.Red));
public System.Windows.Media.Color HeaderColor {
get { return (System.Windows.Media.Color)GetValue(HeaderColorProperty); }
set { SetValue(HeaderColorProperty, value); }
}
}}
I have had mixed results using TemplateBinding on custom dependency properties. Because of this, I have used RelativeSource TemplatedParent which seems to work in every situation.
<EasingColorKeyFrame KeyTime="0:0:.5" Value="{Binding HeaderColor, RelativeSource={RelativeSource TemplatedParent}}" />
这篇关于设置边框背景与模板结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!