【问题标题】:Setting Border background with a template binding使用模板绑定设置​​边框背景
【发布时间】:2011-10-16 08:54:24
【问题描述】:

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

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

【问题讨论】:

  • 你能发布更多代码吗?您实际上在 XAML 中的哪个位置使用此 ColorAnimationUsingKeyFrames?在选择控件的控件模板内?如果是这样,TemplateBinding 应该可以工作!
  • 嗨 AngelWPF 我在我的 generic.xaml 文件中添加了代码,并在我的原始问题下方的控件类中添加了代码。谢谢你的帮助。是的,我在模板中使用了 ColorAnimationUsingKeyFrames,我打赌我错过了一些明显的东西。
  • 您能观察到模板绑定是否在您的 Visual Studio 输出窗口中失败了吗?当动画应该运行时,您是否收到绑定错误?如果是,那么您可以尝试用普通绑定替换模板绑定并使用 RelativeSource={RelativeSource AncestorType={x:Type ctrl:Selection}} insetad...
  • 您可以尝试正常绑定并使用RelativeSource={RelativeSource AncestorType={x:Type ctrl:Selection}} 代替TemplateBinding 吗?
  • 不应该是

标签: silverlight colors background templatebinding


【解决方案1】:

我在自定义依赖属性上使用 TemplateBinding 的结果好坏参半。因此,我使用了似乎在任何情况下都适用的 RelativeSource TemplatedParent。

<EasingColorKeyFrame KeyTime="0:0:.5" Value="{Binding HeaderColor, RelativeSource={RelativeSource TemplatedParent}}" />

【讨论】:

    猜你喜欢
    • 2015-03-07
    • 2014-07-29
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多