WPF-Control/WPF绑定元素-1-代码模式的绑定/MainWindow.xaml.cs

45 lines
1.1 KiB
C#

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WPF绑定元素_1_代码模式的绑定;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Console.WriteLine("使用代码模式的绑定");
var binding = new Binding
{
Source = SliderFontSize,
Path = new PropertyPath("Value"),
Mode = BindingMode.TwoWay
};
TextBlockShow.SetBinding(TextBlock.FontSizeProperty, binding);
}
private void SetLarge_OnClick(object sender, RoutedEventArgs e)
{
SliderFontSize.Value = 100;
}
private void SetNormal_OnClick(object sender, RoutedEventArgs e)
{
SliderFontSize.Value = 40;
}
private void SetSmall_OnClick(object sender, RoutedEventArgs e)
{
SliderFontSize.Value = 10;
}
private void ClearBinding_OnClick(object sender, RoutedEventArgs e)
{
BindingOperations.ClearAllBindings(TextBlockShow);
}
}