LoginSignup
13

More than 5 years have passed since last update.

[WPF][XAML] デザインモード時のみ有効な背景色を設定する

Posted at

WPFのUserControlの背景は基本的に透明にしているが、デザイナーモード時のみ見やすい背景色を設定したい場合の設定方法メモ

設定例

XAML
<UserControl x:Class="TestPrj.View.TestCtrl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:TestPrj.View"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <d:DesignerProperties.DesignStyle>
        <Style TargetType="UserControl">
            <Setter Property="Background" Value="LightGray"/>
        </Style>
    </d:DesignerProperties.DesignStyle>
    <Grid>
        <TextBlock Text="hogehoge"></TextBlock>
    </Grid>
</UserControl>

<d:DesignerProperties.DesignStyle>...</d:DesignerProperties.DesignStyle> の間に
デザイナーモード時のみ有効な任意のStyle設定を追加できます。

今回は背景色のみですが、Style内に同様に任意のプロパティ設定を追加できます。
ただし、たくさん設定する場合はViewModelごとデザイナーモード用のものに入れ替えたほうがよいかもしれないです。
(MVVMフレームワークを使用しているならViewModelLocatorなどに切り替え機能があるはずかと思います)

参考

visual studio 2010 - Design-time-only background color in WPF? - Stack Overflow

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13