以App.xaml样式WPF访问用户控件属性

时间:2018-10-23 13:29:20

标签: wpf app.xaml relativesource visualbrush

我试图通过模型中的属性在可视笔刷(在App.xaml中定义)中设置Path的“笔画”属性。我正在用户控件的另一个控件模板中使用这种样式。

用户控件资源中的样式:

<Ellipse x:Name="slideThumb" Height="25" Width="25" Stroke="{Binding ThumbColor, UpdateSourceTrigger=PropertyChanged}">
                        <Ellipse.Style>
                            <Style TargetType="{x:Type Ellipse}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsHatchBrush}" Value="true">
                                        <Setter Property="Fill" Value="{StaticResource HatchBrushVertical}" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding IsHatchBrush}" Value="false">
                                        <Setter Property="Fill" Value="{Binding ThumbColor, UpdateSourceTrigger=PropertyChanged}" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Ellipse.Style>
                    </Ellipse>

在App.xaml中定义的画笔:

<VisualBrush x:Key="HatchBrushVertical" TileMode="Tile" Viewport="0,0,2,3" ViewportUnits="Absolute" Viewbox="0,0,5,5" ViewboxUnits="Absolute">
        <VisualBrush.Transform>
            <RotateTransform Angle="45" />
        </VisualBrush.Transform>
        <VisualBrush.Visual>
            <Canvas>
                <Path Data="M 0 0 L 0 10" Stroke="{Binding Path=Stroke, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Ellipse}}}" StrokeThickness="5"/>
            </Canvas>
        </VisualBrush.Visual>
    </VisualBrush>

问题是,我无法通过绑定在app.xaml中设置面刷的Stroke属性。

需要帮助。预先感谢

1 个答案:

答案 0 :(得分:0)

您也许可以使用笔刷作为不透明蒙版。

首先,创建一个(简单的)DrawingBrush而不是VisualBrush:

<DrawingBrush x:Key="HatchBrush" TileMode="Tile"
              Viewport="0,0,3,3" ViewportUnits="Absolute"
              Viewbox="0,0,2,2" ViewboxUnits="Absolute">
    <DrawingBrush.Drawing>
        <GeometryDrawing Geometry="M-1,2 L2,-1 M0,3 L3,0">
            <GeometryDrawing.Pen>
                <Pen Thickness="0.7" Brush="Black"/>
            </GeometryDrawing.Pen>
        </GeometryDrawing>
    </DrawingBrush.Drawing>
</DrawingBrush>

然后编写如下所示的DataTrigger:

<DataTrigger Binding="{Binding IsHatchBrush}" Value="True">
    <Setter Property="OpacityMask" Value="{StaticResource HatchBrush}" />
</DataTrigger>