wpf - 在 VisualBrush 内部时忽略 Canvas 背景

标签 wpf xaml background drawing

鉴于此 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>
    ...

Canvas 背景将被忽略,相反,路径在对密码框后面的表单透明的背景上可见。那么我应该在哪里设置“背景的背景”?

最佳答案

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

当然,如果您要绑定(bind)到 View 模型,您也可以通过绑定(bind)来驱动维度。

关于wpf - 在 VisualBrush 内部时忽略 Canvas 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12645257/

相关文章:

c# - 显示分层数据的 WPF 组合框

c# - 混淆包含数据绑定(bind)到内部类属性的程序集

wpf - 通过WPF MVVM处理CommandParameter

c# - 在共享 (.shproj) 项目中添加引用

.net - 如何从 RadButton 关闭 RadWindow

xaml - Windows 8.1 如何自动换行网格项?

c# - Silverlight - 从部分中删除隐式样式

ios - iPad Mini 后台刷新不起作用,其他运行 iOS 8.1 的设备工作正常

java - 需要使用 IntelliJ 更改 GUI 的背景,但背景隐藏了按钮

android - Android定时后台任务最佳实践