Q092. Binding.RelativeSource ã®ä½¿ãæ¹ãããããããªã
Aï¼Binding.RelativeSource ã®ä½¿ãæ¹ããã¾ãã¡ããããããªãã£ãã®ã§ããµã³ãã«ã使ã£ã¦ã¾ã¨ãã¦ã¿ã¾ããã
MSDN ã®èª¬æã«ããã¨
ãã¤ã³ãã£ã³ã° ã¿ã¼ã²ããã®ä½ç½®ã«å¯¾ãã¦ç¸å¯¾çãªä½ç½®ãæå®ãããã¨ã«ããããã¤ã³ãã£ã³ã° ã½ã¼ã¹ãåå¾ã¾ãã¯è¨å®ãã¾ãã
ã¨ããã¨ãããç¸å¯¾çãªä½ç½®é¢ä¿ã«ããè¦ç´ ãåç §ãããããã£ããã¤ã³ããã¾ããä¾ãã° {RelativeSource Self} ã¯èªèº«ãåç §ãã{RelativeSource FindAncestor} ã§ã¯èªèº«ã®å ç¥ã»ã»ã»ããããä¸ä½ã®è¦ç´ ãåç §ãããã¨ã«ãªãã¾ãã
ã§ãç解ãå©ããããããµã³ãã«ãæ¸ãã¦ã¿ã¾ããã
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="305" Width="300" Background="Red"> <Grid Background="Blue" > <Grid Margin="10" Background="Aqua" > <Border CornerRadius="30" Background="LightGray" Margin="10"> <StackPanel Margin="10" Background="Green"> <TextBox Margin="8" BorderBrush="Red" Text="{Binding BorderBrush, RelativeSource={RelativeSource Self}}" Foreground="{Binding Path=BorderBrush, RelativeSource={RelativeSource Self}}" /> <TextBox Margin="8" Text="{Binding Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}" Foreground="{Binding Path=Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}" /> <TextBox Margin="8" Text="{Binding Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border}}}" Foreground="{Binding Path=Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border}}}" /> <TextBox Margin="8" Text="{Binding Background, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Grid}}}" Foreground="{Binding Path=Background, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Grid}}}" /> <TextBox Margin="8" Text="{Binding Background, RelativeSource={RelativeSource FindAncestor, AncestorLevel=2, AncestorType={x:Type Grid}}}" Foreground="{Binding Path=Background, RelativeSource={RelativeSource FindAncestor, AncestorLevel=2, AncestorType={x:Type Grid}}}" /> </StackPanel> </Border> </Grid> </Grid> </Window>
ãã®ãµã³ãã«ã®è¦ç´ ããªã¼ãå³ã«ããã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
1çªç®ã®TextBox ã§ã¯ã{RelativeSource Self} ã使ã£ã¦èªèº«ãåç §ãText ããããã£ã¨Foregroundããããã£ãèªèº«ã® BorderBrush ãããã㣠ã¨ãã¤ã³ãããã¦ã¾ãã
<TextBox Margin="8" BorderBrush="Red" Text="{Binding BorderBrush, RelativeSource={RelativeSource Self}}" Foreground="{Binding Path=BorderBrush, RelativeSource={RelativeSource Self}}" />
2çªç®ã®TextBox ã§ã¯ã{RelativeSource FindAncestor} ã使ããä¸ä½è¦ç´ (å
ç¥è¦ç´ )ã®å StackPanel ãæ¤ç´¢ããText ããããã£ã¨Foregroundããããã£ã StackPanel ã® Background ãããã㣠ã¨ãã¤ã³ãããã¦ã¾ãã
<TextBox Margin="8" Text="{Binding Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}" Foreground="{Binding Path=Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}" />
3çªç®ã®TextBox ãåãã {RelativeSource FindAncestor} ã使ããä¸ä½è¦ç´ (å
ç¥è¦ç´ )ã®å Border ãåç
§ããText ããããã£ã¨Foregroundããããã£ã Border ã® Background ãããã㣠ã¨ãã¤ã³ãããã¦ã¾ãã
<TextBox Margin="8" Text="{Binding Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border}}}" Foreground="{Binding Path=Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border}}}" />
4çªç®ã®TextBox ã {RelativeSource FindAncestor} ã使ããä¸ä½è¦ç´ (å
ç¥è¦ç´ )ã®å Grid ãåç
§ããText ããããã£ã¨Foregroundããããã£ã Grid ã® Background ããããã£ã¨ãã¤ã³ãããã¦ã¾ãããã ãä¸ä½ã®è¦ç´ ã«ã¯ Grid ãäºã¤åå¨ãã¾ããããã§ãAncestorLevel ã« 1 ãè¨å®ããèªèº«ãåºç¹ã«ãã¦1çªç®ã«è¦ã¤ãã£ã Grid ãæå®ãã¾ãã
<TextBox Margin="8" Text="{Binding Background, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Grid}}}" Foreground="{Binding Path=Background, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Grid}}}" />
5çªç®ã®TextBox ã {RelativeSource FindAncestor} ã使ããä¸ä½è¦ç´ (å
ç¥è¦ç´ )ã®å Grid ãåç
§ããText ããããã£ã¨Foregroundããããã£ã Grid ã® Background ããããã£ã¨ãã¤ã³ãããã¦ã¾ãããã ãä¸ä½è¦ç´ ã«ã¯ Grid ãäºã¤åå¨ããã®ã§ãAncestorLevel ã« 2 ãè¨å®ããèªèº«ãåºç¹ã«ãã¦2çªç®ã«è¦ã¤ãã£ã Grid ãæå®ãã¾ãã
<TextBox Margin="8" Text="{Binding Background, RelativeSource={RelativeSource FindAncestor, AncestorLevel=2, AncestorType={x:Type Grid}}}" Foreground="{Binding Path=Background, RelativeSource={RelativeSource FindAncestor, AncestorLevel=2, AncestorType={x:Type Grid}}}" />
åèè¨äº ï¼ RelativeSource のマークアップ拡張機能
åèè¨äº ï¼ 第5回 WPFの「データ・バインディング」を理解する
åèè¨äº ï¼ BindingのRelativeSourceの設定色々
ã