绑定到generic.xaml中的readonly属性,用于customControl(Button)

时间:2014-10-21 08:21:04

标签: wpf vb.net xaml custom-controls dependency-properties

我正在使用generic.xaml和依赖项属性执行CustomControl(按钮)。

这是我的generic.xaml代码:

<Style TargetType="{x:Type local:FlatButton}">
     <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:FlatButton}">
                <Grid MinHeight="50" MaxHeight="50" MinWidth="200" MaxWidth="200">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="3*" />
                    </Grid.ColumnDefinitions>     

                    <Grid Grid.Column="0" Background="{TemplateBinding BackgroundDarker}">

                    </Grid>
                    <Grid Grid.Column="1" Background="{TemplateBinding Background}">
                        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Segoe UI" Foreground="White" FontWeight="Bold" />
                    </Grid>

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我的customControl类:

Public Shared Shadows BackgroundProperty As DependencyProperty = DependencyProperty.Register("Background", GetType(SolidColorBrush), GetType(FlatButton))
Public Overloads Property Background As SolidColorBrush
    Get
        Return CType(GetValue(BackgroundProperty), SolidColorBrush)
    End Get
    Set(value As SolidColorBrush)
        SetValue(BackgroundProperty, value)
    End Set
End Property

Public Shared BackgroundDarkerProperty As DependencyProperty = DependencyProperty.Register("BackgroundDarker", GetType(SolidColorBrush), GetType(FlatButton))
Public ReadOnly Property BackgroundDarker As SolidColorBrush
    Get
        Return Background.Darker
    End Get
End Property

最后我如何在UserControl中使用我的控件:

<Grid>
    <local:FlatButton Background="Red" />
</Grid>

当我把&#34; Red&#34;在我的FlatButton的xaml中,正确的部分在红色(在VS和运行时)中很好地着色,但我想要的是左边部分自动着色为深红色(它是一个有效的扩展)。但似乎没有色彩。我输出中没有绑定错误。

我做错了什么?

谢谢大家。

-----编辑-----

好的,为此我做了一个转换器来转换&#34;背景&#34;值得更深的颜色。 我将左边网格的背景模板绑定到&#34;背景&#34;与我的转换器的实例。

1 个答案:

答案 0 :(得分:0)

转换器工作时,此功能属于FlatButton控制代码。使用背景的PropertyChangedCallback更新BackgroundDarker。

enter image description here enter image description here enter image description here

public class FlatButton : Button
{
    // Background
    public static new DependencyProperty BackgroundProperty = DependencyProperty.Register("Background", typeof(SolidColorBrush), typeof(FlatButton), new PropertyMetadata(OnBackgroundChanged));
    public new SolidColorBrush Background { get { return (SolidColorBrush)GetValue(BackgroundProperty); } set { SetValue(BackgroundProperty, value); } }

    // BackgroundDarker
    public static DependencyProperty BackgroundDarkerProperty = DependencyProperty.Register("BackgroundDarker", typeof(SolidColorBrush), typeof(FlatButton));
    public SolidColorBrush BackgroundDarker { get { return (SolidColorBrush)GetValue(BackgroundDarkerProperty); } private set { SetValue(BackgroundDarkerProperty, value); } }

    private static void OnBackgroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // update BackgroundDarker
        var btn = (FlatButton)d;
        btn.BackgroundDarker = btn.Background.Darker();
    }

    static FlatButton()
    {
        // lookless control, get default style from generic.xaml
        DefaultStyleKeyProperty.OverrideMetadata(typeof(FlatButton), new FrameworkPropertyMetadata(typeof(FlatButton)));
    }
}

public static class SolidColorBrushExtension
{
    public static SolidColorBrush Darker(this SolidColorBrush brush)
    {
        const double perc = 0.6;
        return new SolidColorBrush(Color.FromRgb((byte)(brush.Color.R * perc), (byte)(brush.Color.G * perc), (byte)(brush.Color.B * perc)));
    }
}