WPF Dependency 属性 中是否每个 属性?
Is every Property in WPF Dependency Property?
我阅读了 this post and this one 有关依赖属性的内容,但我很困惑,所有属性都在 XAML 依赖属性中吗?那么我们在 C# 中定义的普通属性呢?我的意思是这样的:Public int num {get; set;}
.
因为 C# 中的普通属性有一些功能,它们作为依赖选项提到 属性 例如,我可以将它们的值绑定到文本框值。
如果你能做一个简单的例子,我将不胜感激。
我觉得其他帖子回答了什么是依赖关系 属性 相当不错,所以我将解决您的问题,展示如何建立依赖关系 属性,希望这会有所帮助。
Are all properties in XAML dependency properties?
否,依赖属性必须如此指定。见下文...
public class MyDataGridControl : DataGrid
{
public string SomeName
{
get { return (string)GetValue(SomeNameProperty); }
set { SetValue(SomeNameProperty, value); }
}
public static readonly DependencyProperty SomeNameProperty =
DependencyProperty.Register(
nameof(SomeName), typeof(string), typeof(MyDataGridControl),
new PropertyMetadata(null));
}
在上面的示例中,我创建了一个 class 继承自 DataGrid 以创建我自己的 DataGrid 控件。我创建了 "normal property" SomeName。然后我将 SomeName 注册为依赖项 属性。请注意,虽然 SomeName 是 "normal property",但 getter 和 setter 正在引用 SomeName属性 依赖项 属性.
我阅读了 this post and this one 有关依赖属性的内容,但我很困惑,所有属性都在 XAML 依赖属性中吗?那么我们在 C# 中定义的普通属性呢?我的意思是这样的:Public int num {get; set;}
.
因为 C# 中的普通属性有一些功能,它们作为依赖选项提到 属性 例如,我可以将它们的值绑定到文本框值。
如果你能做一个简单的例子,我将不胜感激。
我觉得其他帖子回答了什么是依赖关系 属性 相当不错,所以我将解决您的问题,展示如何建立依赖关系 属性,希望这会有所帮助。
Are all properties in XAML dependency properties?
否,依赖属性必须如此指定。见下文...
public class MyDataGridControl : DataGrid
{
public string SomeName
{
get { return (string)GetValue(SomeNameProperty); }
set { SetValue(SomeNameProperty, value); }
}
public static readonly DependencyProperty SomeNameProperty =
DependencyProperty.Register(
nameof(SomeName), typeof(string), typeof(MyDataGridControl),
new PropertyMetadata(null));
}
在上面的示例中,我创建了一个 class 继承自 DataGrid 以创建我自己的 DataGrid 控件。我创建了 "normal property" SomeName。然后我将 SomeName 注册为依赖项 属性。请注意,虽然 SomeName 是 "normal property",但 getter 和 setter 正在引用 SomeName属性 依赖项 属性.