Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

チェックボックスの外観をブラシのソースとして使用する

アプリ実行

起動


マウスオーバーでチェックボックスの外観が変わると追随する


ソースコード

App.xaml
<Application x:Class="HelloWorld.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
</Application>
MainWindow.xaml
<Window x:Class="HelloWorld.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="500" Width="525">
</Window>
MainWindow.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace HelloWorld {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();

            CheckBox checkBox = new CheckBox();
            checkBox.Content = "CheckBox";
            checkBox.Width = 100;
            stackPanel.Children.Add(checkBox);

            {   // チェックボックスを敷き詰める
                Rectangle rect = new Rectangle();
                rect.Margin = new Thickness(5);
                rect.Width = 200;
                rect.Height = 200;
                rect.Stroke = Brushes.Black;
                rect.StrokeThickness = 5;
                VisualBrush brush = new VisualBrush();
                brush.Visual = checkBox;
                brush.TileMode = TileMode.Tile;
                brush.Stretch = Stretch.Uniform;
                brush.Viewport = new Rect(0, 0, 50, 20);
                brush.ViewportUnits = BrushMappingMode.Absolute;
                rect.Fill = brush;
                stackPanel.Children.Add(rect);
            }
            {   // チェックボックスで塗りつぶす
                Rectangle rect = new Rectangle();
                rect.Margin = new Thickness(5);
                rect.Width = 500;
                rect.Height = 200;
                rect.Stroke = Brushes.Black;
                rect.StrokeThickness = 5;
                VisualBrush brush = new VisualBrush();
                brush.Visual = checkBox;
                rect.Fill = brush;
                stackPanel.Children.Add(rect);
            }

            Content = stackPanel;
        }
    }
}

参考

TileBrush.Viewport プロパティ (System.Windows.Media)
TileBrush の基本タイルの位置とサイズを取得または設定します。

TileBrush.ViewportUnits プロパティ (System.Windows.Media)
TileBrush 基本タイルのサイズと位置を示す Viewport の値が出力領域のサイズに対して相対的かどうかを指定する、BrushMappingMode 列挙体を取得または設定します。