【问题标题】:Canvas background ignored when inside of a VisualBrush在 VisualBrush 内部时忽略画布背景
【发布时间】:2012-09-28 18:15:32
【问题描述】:

鉴于此 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 后面的表单透明的背景上可见。那么我应该在哪里设置“背景的背景”?

【问题讨论】:

  • 我想将它设置为WindowBrush 使它与它后面的表单相同。你确定它不只是相同的颜色吗?你试过Background="Red"之类的吗?
  • WindowBrush 评估为不透明颜色。红色也会被忽略。
  • Path.Fill上设置那个画笔怎么样?
  • 刚试过;这也被忽略和透明。

标签: wpf xaml background drawing


【解决方案1】:

问题是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}" >

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

【讨论】:

  • 像魅力一样工作。但是,我的下一个问题是 - 我怎样才能尽可能多地消除对“10”的引用?我可以以某种方式使用相对单位并在除(比方说)视口之外的任何地方都写“1”吗?
  • @Reinderien 已添加到答案中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-08
  • 1970-01-01
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多