silverlight – 设置边框背景与模板绑定

silverlight – 设置边框背景与模板绑定,第1张

概述Value =“{TemplateBinding HeaderColor}”我创建了自己的控件,我想知道是否可以将Border.Background绑定到模板属性.目前我正在使用如下的StaticResource设置它: <Color x:Key="ControlMouseOverColor">green</Color><ColorAnimationUsingKeyFrames Storybo Value =“{TemplateBinding headercolor}”我创建了自己的控件,我想知道是否可以将border.Background绑定到模板属性.目前我正在使用如下的StaticResource设置它:

<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>

我希望它是我控制的属性,并能够将其设置为模板绑定

<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" />

我的课:

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); }}

如果我能够这样做,第二个选项不起作用?我没有收到错误,它只是没有改变我设置的颜色.

AngelWPF留下的评论要求更多代码,粘贴在下面,我正处于学习创建控件的开始阶段,想要注意,因为有很多我还没有,一次一件:)

generic.xaml

<ResourceDictionaryxmlns="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" />    <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" 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),new PropertyMetadata(""));    public string headerText {        get { return (string)GetValue(headerTextProperty); }        set { SetValue(headerTextProperty,value); }    }    public static Readonly DependencyProperty headercolorProperty =        DependencyProperty.Register("headercolor",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); }    }}}
解决方法 我在自定义依赖项属性上使用TemplateBinding得到了混合结果.因此,我使用了relativeSource TemplatedParent,它似乎适用于所有情况.

<EasingcolorKeyFrame KeyTime="0:0:.5" Value="{Binding headercolor,relativeSource={relativeSource TemplatedParent}}" />
总结

以上是内存溢出为你收集整理的silverlight – 设置边框背景与模板绑定全部内容,希望文章能够帮你解决silverlight – 设置边框背景与模板绑定所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1005463.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存