LoginSignup
0
1

More than 5 years have passed since last update.

Visual Studio / WPF > binding > RelativeSource={RelativeSource Self} > 複数のコントロールの色をNameで指定する

Last updated at Posted at 2017-06-15
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community

RelativeSource={RelativeSource Self}
という書き方にまだ慣れていない。

参考

実装

x:Nameに色を指定して、その文字列に基づいて色をつける実装をしてみた。
Implicit Key(x:Key未指定)でStyle指定。

MainWindow.xaml
<Window x:Class="_170615_t1700_relativeSource.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_170615_t1700_relativeSource"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="Rectangle">
            <Setter Property="Fill"
                    Value="{Binding Name, RelativeSource={RelativeSource Self}}"/>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Rectangle x:Name="Yellow" Height="50"/>
            <Rectangle x:Name="Red" Height="50"/>
            <Rectangle x:Name="Blue" Height="50"/>
        </StackPanel>
    </Grid>
</Window>

2017-06-15_17h14_02.png

将来、どういう場面でRelativeSource={RelativeSource Self}を必要とするかは今のところ不明。

0
1
0

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
0
1