tl; dr:强制值不会跨数据绑定传播.当代码隐藏不知道绑定的另一面时,如何强制更新数据绑定?
我正在使用一个CoerceValueCallback
WPF依赖属性,我坚持认为强制值不会传播到绑定的问题.
Window1.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace CoerceValueTest
{
public class SomeControl : UserControl
{
public SomeControl()
{
StackPanel sp = new StackPanel();
Button bUp = new Button();
bUp.Content = "+";
bUp.Click += delegate(object sender, RoutedEventArgs e) {
Value += 2;
};
Button bDown = new Button();
bDown.Content = "-";
bDown.Click += delegate(object sender, RoutedEventArgs e) {
Value -= 2;
};
TextBlock tbValue = new TextBlock();
tbValue.SetBinding(TextBlock.TextProperty,
new Binding("Value") { …
Run Code Online (Sandbox Code Playgroud)