首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从DependencyProperty更新辅助属性

从DependencyProperty更新辅助属性
EN

Stack Overflow用户
提问于 2017-07-10 11:04:39
回答 2查看 23关注 0票数 0

我有一个基于TextBox控件的WPF控件:

代码语言:javascript
运行
复制
public class DecimalTextBox : TextBox

我有一个绑定到的依赖项属性,它管理数值,并负责设置Text属性:

代码语言:javascript
运行
复制
public decimal NumericValue
{
    get { return (decimal)GetValue(NumericValueProperty); }
    set
    {
        if (NumericValue != value)
        {
            SetValue(NumericValueProperty, value);
            SetValue(TextProperty, NumericValue.ToString());                    

            System.Diagnostics.Debug.WriteLine($"NumericValue Set to: {value}, formatted: {Text}");
        }
    }
}

protected override void OnTextChanged(TextChangedEventArgs e)
{            
    base.OnTextChanged(e);

    if (decimal.TryParse(Text, out decimal num))
    {
        SetValue(NumericValueProperty, num);
    }
}

当将一个值输入到textbox本身时(它更新基础值等.),这是很好的。但是,当更改NumericValue的绑定属性时,尽管更新了NumericValue DP,但NumericValue属性仍未更新。在我所做的测试中,原因似乎是在更新绑定值时没有调用上面的set方法。所讨论的绑定如下:

代码语言:javascript
运行
复制
<myControls:DecimalTextBox NumericValue="{Binding Path=MyValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

有人能告诉我为什么这位地产策划人没有开火,或者有更好的方法来解决这个问题吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-10 11:17:16

正如在自定义依赖属性XAML加载和依赖属性中所解释的,在依赖项属性的CLR包装器中,除了GetValueSetValue之外,不应该调用其他任何东西:

由于属性设置的XAML处理器行为的当前WPF实现完全绕过包装器,因此不应将任何附加逻辑放入自定义依赖项属性的包装器的集定义中。如果将此逻辑放入set定义中,则当属性在XAML中而不是在代码中设置时,将不会执行该逻辑。

为了得到有关值更改的通知,您必须向依赖项属性元数据注册一个PropertyChangedCallback

代码语言:javascript
运行
复制
public static readonly DependencyProperty NumericValueProperty =
    DependencyProperty.Register(
        "NumericValue", typeof(decimal), typeof(DecimalTextBox),
        new PropertyMetadata(NumericValuePropertyChanged));

public decimal NumericValue
{
    get { return (decimal)GetValue(NumericValueProperty); }
    set { SetValue(NumericValueProperty, value); }
}

private static void NumericValuePropertyChanged(
    DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    var textBox = (DecimalTextBox)obj;
    textBox.Text = e.NewValue.ToString();
}
票数 1
EN

Stack Overflow用户

发布于 2017-07-10 11:11:05

WPF绑定实际上不是使用getter和setter,而是直接与依赖项属性NumericValueProperty交互。为了更新文本,请订阅NumericValuePropertyNumericValueProperty事件,而不是尝试在setter中执行任何特殊的操作。

订阅DependencyProperty定义中的更改,类似于以下内容:

代码语言:javascript
运行
复制
// Using a DependencyProperty as the backing store for NumericValue.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty NumericValueProperty =
    DependencyProperty.Register("NumericValue", typeof(decimal), typeof(DecimalTextBox), new FrameworkPropertyMetadata(0.0m, new PropertyChangedCallback(OnNumericValueChanged)));

private static void OnNumericValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var self = d as DecimalTextBox;
    // if the new numeric value is different from the text value, update the text
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45010390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档