WPF样式 - 使用DependencyProperty作为CONST值

时间:2014-07-25 16:42:58

标签: c# xaml binding dependency-properties visualstatemanager

我用图标和文本构建自己的Button,传递DependencyProperty颜色,图标路径和文本。到目前为止没问题。这是color属性之一的代码。

    //Text Color
    public static readonly DependencyProperty forColorProperty =
               DependencyProperty.Register(
                   "forColor",
                   typeof(System.Windows.Media.SolidColorBrush),
                   typeof(AppButtonPro),
                   new PropertyMetadata(Colors.DarkBlue));


    public System.Windows.Media.SolidColorBrush forColor
    {
        get { return (System.Windows.Media.SolidColorBrush)GetValue(forColorProperty);}
        set { SetValue(forColorProperty, value); }
    }

和XAML代码:

       <ap:AppButtonPro Style="{DynamicResource AppButtonFast}" 
            forColor="{Binding Source={x:Static const:Colors.White}}" 
            backColor="{Binding Source={x:Static const:Colors.LightBlue}}"
            Grid.Column="0" 
            Grid.Row="1" 
            Grid.RowSpan="1" 
            Grid.ColumnSpan="3" 
            x:Name="btScrollUp" 
            Click="btScrollUp_Click"
            textContentString="Text"
            iconContentString="pack://application:,,,/DllData;component/Appearence/down.png"
            >
    </ap:AppButtonPro>

我想在点击时交换颜色以增强对比度,所以我想将前景色分配给背景,反之亦然。我假设我不能简单地反转绑定,所以我想将颜色值存储在样式中的某个位置,并将它们用作局部变量以在状态中播放。 例如,这是一个带有前景色的文本块,它与XAML中声明的forColor值绑定:我想在某些visualstate中将其更改为backColor的值,并在需要时将其更改回正常值。

 <TextBlock x:Name="textContent" 
                        VerticalAlignment="Center" 
                        HorizontalAlignment="Center" 
                        Text="{Binding textContentString, RelativeSource={RelativeSource TemplatedParent}}" 
                        Foreground="{Binding forColor, RelativeSource={RelativeSource TemplatedParent}}"/>

0 个答案:

没有答案