1

以前の質問で、kbmax は、generic.xaml でカスタム プロパティをバインドする方法を教えてくれました。この回答は、テンプレートバインディングを使用したボーダー背景の設定にありました

LinearGradientBrush StartPoint でこれを実行しようとするまではうまくいきました。例を次に示します。

これは明らかにうまくいきます:

<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">

次のようにカスタム プロパティにバインドしたいと思います。これは機能しません。プロパティを何に設定しても、常に左から右へのグラデーションになります。

<LinearGradientBrush StartPoint="{Binding HeaderBorderGradientStartPoint, RelativeSource={RelativeSource TemplatedParent}}" EndPoint="{Binding HeaderBorderGradientEndPoint, RelativeSource={RelativeSource TemplatedParent}}">

これがクラスのコードです。

    public static readonly DependencyProperty HeaderBorderGradientStartPointProperty =
        DependencyProperty.Register("HeaderBorderGradientStartPoint", typeof(Point), typeof(GalleryExpander), new PropertyMetadata(new Point(0.5,0)));
    public Point HeaderBorderGradientStartPoint {
        get { return (Point)GetValue(HeaderBorderGradientStartPointProperty); }
        set { SetValue(HeaderBorderGradientStartPointProperty, value); }
    }

    public static readonly DependencyProperty HeaderBorderGradientEndPointProperty =
        DependencyProperty.Register("HeaderBorderGradientEndPoint", typeof(Point), typeof(GalleryExpander), new PropertyMetadata(new Point(0.5,1)));
    public Point HeaderBorderGradientEndPoint {
        get { return (Point)GetValue(HeaderBorderGradientEndPointProperty); }
        set { SetValue(HeaderBorderGradientEndPointProperty, value); }
    }


<Border.BorderBrush>
    <LinearGradientBrush StartPoint="{Binding HeaderBorderGradientStartPoint, RelativeSource={RelativeSource TemplatedParent}}" EndPoint="{Binding HeaderBorderGradientEndPoint, RelativeSource={RelativeSource TemplatedParent}}">
        <LinearGradientBrush.GradientStops>
            <GradientStop Offset="0.0" Color="Transparent" />
            <GradientStop Offset="0.25" Color="Transparent" />
            <GradientStop Offset="0.50" Color="Transparent" />
            <GradientStop Offset="0.75" Color="Transparent" />
            <GradientStop Offset="1.0" Color="Transparent" />
        </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>
</Border.BorderBrush>

方向性をありがとう...

4

1 に答える 1

2

うーん、これを確認するために小さなカスタム コントロールを作成しましたが、問題なく動作します。私のコントロールは次のようになります。

public class MyGradientControl : Control
{
    public static readonly DependencyProperty StartPointDProperty =
        DependencyProperty.Register("StartPointD", typeof (Point), 
        typeof (MyGradientControl), new PropertyMetadata(new Point(0.5, 0.5)));

    static MyGradientControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof (MyGradientControl), 
            new FrameworkPropertyMetadata(typeof (MyGradientControl)));
    }

    public Point StartPointD
    {
        get { return (Point) GetValue(StartPointDProperty); }
        set { SetValue(StartPointDProperty, value); }
    }
}

次のスタイルがあります (Themes\generic.xaml 内):

<Style TargetType="{x:Type local:MyGradientControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyGradientControl}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid VerticalAlignment="Stretch" 
                          HorizontalAlignment="Stretch">
                        <Grid.Background>
                            <LinearGradientBrush 
                                StartPoint="{Binding StartPointD, 
                                             RelativeSource={RelativeSource 
                                                  TemplatedParent}}" 
                                EndPoint="0, 0">
                                <GradientStop Color="Red" Offset="0"/>
                                <GradientStop Color="White" Offset="1"/>
                            </LinearGradientBrush>
                        </Grid.Background>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

LinearGradientBrush に GradientStops を提供していますか? オフセットはありますか?

于 2011-08-10T21:51:22.160 に答える