.Net Framework WPF

WPF バインディング:Source の使い方

2017年11月17日

Binding オブジェクトの持つ、Source プロパティの利用方法の紹介です。Source プロパティを利用することで、Resources に登録されている任意のオブジェクトを参照することができます。

例えば、Resources に用意されている Int 型の値を、TextBox の Tooltip プロパティにバインドしてみます。

※ Int 型の値を利用するためには、mscorlib アセンブリの参照を追加しています。

<Window x:Class="WpfApp3.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:WpfApp3"
mc:Ignorable="d"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow X" Height="350" Width="525">
<Grid>
<Grid.Resources>
<sys:Int32 x:Key="num1">123</sys:Int32>
</Grid.Resources>

<TextBox x:Name="textBox1"
Height="30"
Width="250"
Text="abc"
ToolTip="{Binding Source={StaticResource num1}}"/>
</Grid>
</Window>

-.Net Framework, WPF