[10 行でズバリ!! [C#] コントロール間の連携]をダウンロードする。
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="LayoutRoot" Background="LightCyan">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox x:Name="textbox1" Margin="5"
Text="スライダーでフォント サイズを変更できます。" />
<Slider Grid.Row="1" x:Name="slider1" Margin="5"
Minimum="10" Maximum="60" Value="16" />
<TextBox Grid.Row="2" x:Name="textbox2" Margin="5" Text="Green" />
<TextBlock Grid.Row="3" Name="textblock1" Text="TextBlock"
Foreground="{Binding ElementName=textBox2, Path=Text}" />
</Grid>
</Window>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Reflection;
namespace Bindings
{
public partial class MainWindow : Window
{
TextBlock textblock0 =new TextBlock();
public MainWindow()
{
InitializeComponent();
}
private void textbox1_TextChanged(object sender, TextChangedEventArgs e)
{
textblock0.Text = textbox1.Text;
}
private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
{
textblock0.FontSize = slider1.Value;
}
private void textbox2_TextChanged(object sender, TextChangedEventArgs e)
{
PropertyInfo pinfo = typeof(Colors).GetProperty(textbox2.Text);
Color c;
if (pinfo == null)
{
c = Colors.Red;
}
else
{
c =(Color) pinfo.GetValue(null, null);
}
textblock0.Foreground = new SolidColorBrush(c);
}
private void textblock1_Loaded(object sender, RoutedEventArgs e)
{
textblock0 = textblock1;
}
}
}