2012-09-28 51 views
4

内部鉴于这一XAML:画布背景忽略当VisualBrush

<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背后的形式可见。那么我应该在哪里设置“背景背景”?

+0

我会想象,将其设置为'WindowBrush'使其与背后的窗体相同。你确定它不只是相同的颜色?你尝试过一些类似'Background =“Red”'的东西吗? – Jay

+0

WindowBrush评估为不透明颜色。红色也被忽略。 – Reinderien

+0

如何在'Path.Fill'上设置画笔? – Jay

回答

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}" > 

当然,如果你是绑定到视图模型,你也可以通过具有约束力的驱动尺寸。

+0

工程就像一个魅力。但是,我的下一个问题是 - 我怎样才能尽可能多地消除对“10”的引用?我能以某种方式使用相对单位,并且除了在视图(比如说)中写入“1”吗? – Reinderien

+0

@Reinderien添加到答案。 – Jay

+0

太棒了!很有用。 – Reinderien