4

鉴于此 XAML:

<Style TargetType="PasswordBox">
    <Setter Property="Background">
        <Setter.Value>
            <VisualBrush TileMode="Tile"
                         Viewport="0,0,10,10" ViewportUnits="Absolute">
                <VisualBrush.Visual>
                    <Canvas Background="{x:Static SystemColors.WindowBrush}">
                        <Path Data="M0,0 L10,10 M0,10 L10,0">
                            <Path.Stroke>
                                <SolidColorBrush Color="{x:Static SystemColors.HighlightColor}"/>
                            </Path.Stroke>
                        </Path>
                    </Canvas>
                </VisualBrush.Visual>
            </VisualBrush>
        </Setter.Value>
    </Setter>
    ...

画布背景被忽略,而是在对 PasswordBox 后面的表单透明的背景上可见路径。那么我应该在哪里设置“背景的背景”?

4

1 回答 1

4

问题是Canvas没有大小。

将其更改为此,您应该会看到它:

<Canvas Background="{x:Static SystemColors.WindowBrush}"
        Width="10"
        Height="10">

要减少对这些维度的引用数量,您可以将它们声明为资源。由于您正在处理正方形,因此可以将其减少到一个值:

    <Grid.Resources>
        <System:Double x:Key="Width">10</System:Double>
        <System:Double x:Key="Height">10</System:Double>
        <Style TargetType="PasswordBox">
            <Setter Property="Background">
                <Setter.Value>
                    <VisualBrush TileMode="Tile"
                                 ViewportUnits="Absolute">
                        <VisualBrush.Viewport>
                            <Rect Width="{StaticResource Width}"
                                  Height="{StaticResource Height}" />
                        </VisualBrush.Viewport>
                        <VisualBrush.Visual>
                            <Canvas Background="{x:Static SystemColors.WindowBrush}" 
                                    Width="{StaticResource Width}"
                                    Height="{StaticResource Height}" >

当然,如果你绑定到一个视图模型,你也可以通过绑定来驱动维度。

于 2012-09-28T18:32:57.593 回答