【问题标题】:Alternative to (obsolete) GetValue and SetValue methods替代(过时的)GetValue 和 SetValue 方法
【发布时间】:2018-07-27 18:41:09
【问题描述】:

我正在尝试获取和设置 DependencyProperty。 DependencyProperty 用于描述正则表达式语句的掩码,用于控制文本框中的用户输入。有问题的代码如下:

public string Mask
{
     get { return (string)GetValue(MaskProperty); }
     set { SetValue(MaskProperty, value); }
}

public static readonly DependencyProperty MaskProperty = 
    DependencyProperty.Register("Mask", typeof(string), typeof(MaskTextBox), new PropertyMetadaa(string.Empty));

一切似乎都在工作,除了 GetValue 和 SetValue。现在,我知道我可以使用 System.Workflow 命名空间来让这些工作,但我也知道命名空间已经过时,所以我宁愿不使用它。

似乎 System.Activities 命名空间被设计为 System.Workflow 的最新替代品,因此考虑到这一点,是否有 System.Activities 替代 GetValue 和 SetValue 方法?如果不是,那么这些方法的有效替代方法是什么?

【问题讨论】:

  • 什么?你的类是从DependencyObject 派生的吗?该类为 WPF 的 Get/SetValue 物流提供了支柱......
  • 类本身派生自 TextBox 对象,是 System.Windows.Forms 命名空间的一部分。
  • WindowsForms != WPF - windform 元素不是从 DependencyObject 派生的 - 您在这里混合了难以混合的技术。

标签: c# .net wpf dependency-properties


【解决方案1】:

应包含在 WPF 绑定机制中的任何类都需要派生自 DependencyObject 或其子类之一:

System.Object
  System.Windows.Threading.DispatcherObject
    System.Windows.DependencyObject
      System.Windows.ContentElement
      System.Windows.Controls.DataGridColumn
      System.Windows.Controls.GridViewColumn
      System.Windows.Controls.Ribbon.Primitives.StarLayoutInfo
      System.Windows.Controls.TextSearch
      System.Windows.Controls.ViewBase
      System.Windows.Data.BindingGroup
      System.Windows.Data.CollectionContainer
      System.Windows.Data.CollectionViewSource
      System.Windows.Freezable
      System.Windows.Ink.GestureRecognizer
      System.Windows.Media.Media3D.Visual3D
      System.Windows.Media.Visual
      System.Windows.Navigation.JournalEntry
      System.Windows.TriggerAction
      System.Windows.TriggerBase
      System.Windows.VisualState
      System.Windows.VisualStateGroup
      System.Windows.VisualStateManager
      System.Windows.VisualTransition

(WPF 将进一步派生的用于 WPF 的东西,如 TextBox 等 - 它们位于 UIElement/FrameWorkElement 之下)。

DependencyObject 提供了实现 DependencyPropertys 所需的 GetValue/SetValue 方法。


您不能简单地将 DependencyProperty 放在 Winforms-Class 中并认为它会起作用 - 您可以这样做,但它不会。根本不存在底层管道。

【讨论】:

  • 这似乎就是我要找的东西!此外,这让我清楚地知道,我非常缺乏对 WPF 和 Winforms 的了解。你知道有什么好的资源可以帮助我更好地理解它吗?
  • @MrSpudtastic - 抱歉,对 wpf 教程一无所知,但 google 应该发布一些,这里要求 tuts 认为 OT 所以最好不要为此使用新问题。您可能还可以查看 channel9 或 microsoft academy 以获取相关材料,但正如我所说:不知道从哪里获得好的材料。也许还可以看看这样的 MS 网站:docs.microsoft.com/en-us/dotnet/framework/wpf/getting-started/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多