实现将TextBox字体大小绑定到Slider的Value属性,代码查询绑定关系。
实现使用代码创建绑定,解除绑定关系
XAML UI代码如下:
<Grid>
<StackPanel Margin="5">
<Slider x:Name="sliderFontSize" Margin="5" Minimum="1" Maximum="40" Value="10" TickPlacement="TopLeft" TickFrequency="1"></Slider>
<TextBlock x:Name="txtSample" Margin="10" FontSize="{Binding ElementName=sliderFontSize,Path=Value,Mode=TwoWay}">测试绑定</TextBlock>
<WrapPanel >
<Button Margin="20" x:Name="btnSetLarge" Click="btnSetLarge_Click">Large</Button>
<Button Margin="20" x:Name="btnSetMiddle" Click="btnSetMiddle_Click">Middle</Button>
<Button Margin="20" x:Name="btnSetSmall" Click="btnSetSmall_Click">Small</Button>
<Button Margin="20" x:Name="btnGetBindingRelate" Click="btnGetBindingRelate_Click">查询绑定关系方式1</Button>
<Button Margin="20" x:Name="btnGetBindingRelate2" Click="btnGetBindingRelate2_Click">查询绑定关系方式2</Button>
</WrapPanel>
<GroupBox Header="使用代码创建绑定" Margin="10">
<StackPanel Margin="5">
<Slider x:Name="slider2" Margin="5" Minimum="1" Maximum="40" Value="10" TickPlacement="Both" TickFrequency="1"></Slider>
<TextBox x:Name="txtSample2" Margin="10">使用代码创建绑定</TextBox>
<Button x:Name="btnCreateBinding" Click="btnCreateBinding_Click" Margin="5">创建绑定</Button>
<Button x:Name="btnCleanBinding" Click="btnCleanBinding_Click" Margin="5">解除绑定</Button>
</StackPanel>
</GroupBox>
</StackPanel>
</Grid>
后台代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnSetLarge_Click(object sender, RoutedEventArgs e)
{
txtSample.FontSize = 35;
}
private void btnSetMiddle_Click(object sender, RoutedEventArgs e)
{
txtSample.FontSize = 15;
}
private void btnSetSmall_Click(object sender, RoutedEventArgs e)
{
txtSample.FontSize = 5;
}
private void btnGetBindingRelate_Click(object sender, RoutedEventArgs e)
{
Binding binding = BindingOperations.GetBinding(txtSample, TextBox.FontSizeProperty);
if (binding != null)
{
string info = $"绑定元素名称:{binding.ElementName}\r\n绑定值:{binding.Path.Path}\r\n绑定模式:{binding.Mode}";
MessageBox.Show(info);
}
}
private void btnGetBindingRelate2_Click(object sender, RoutedEventArgs e)
{
BindingExpression expression=BindingOperations.GetBindingExpression(txtSample, TextBox.FontSizeProperty);
//获取绑定源元素
Slider bindSource = (Slider)expression.ResolvedSource;
var bindData = bindSource.FontSize;
var bindvalue = bindSource.Value;
string info = $"绑定数据源:{bindSource.Name}\r\n绑定值:{bindSource.Value}";
MessageBox.Show(info);
}
// 使用代码创建绑定
private void btnCreateBinding_Click(object sender, RoutedEventArgs e)
{
Binding binding= new Binding();
binding.Source = slider2;
binding.Path = new PropertyPath("Value");
binding.Mode=BindingMode.TwoWay;
txtSample2.SetBinding(TextBox.FontSizeProperty, binding);
}
private void btnCleanBinding_Click(object sender, RoutedEventArgs e)
{
BindingOperations.ClearAllBindings(txtSample2);
}
}
}