如何从代码隐藏构造函数访问 DependencyProperty 值?

发布于 2024-07-21 05:36:29 字数 2150 浏览 3 评论 0原文

我有一个名为 SmartForm 的 UserControl,它有一个名为 Status 的 DependencyProperty。

在我的 Window1.xaml 中,我有元素

我认为在 SmartForm 对象的构造函数中,Status 将等于“Ready”,但它等于 null

为什么 SmartForm 的构造函数中 Status 属性的值为 NULL

如果不在 UserControl 构造函数中,那么我什么时候可以访问该值

Window1.xaml:

<Window x:Class="TestPropertyDefine23282.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestPropertyDefine23282"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <local:SmartForm Status="Ready"/>
    </Grid>
</Window>

SmartForm.xaml:

<UserControl x:Class="TestPropertyDefine23282.SmartForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
        <TextBlock x:Name="TestingMessage"/>
    </Grid>
</UserControl>

SmartForm.xaml.cs:

using System.Windows;
using System.Windows.Controls;

namespace TestPropertyDefine23282
{
    public partial class SmartForm : UserControl
    {
        public SmartForm()
        {
            InitializeComponent();

            TestingMessage.Text = Status; //WHY IS STATUS NOT YET SET HERE?

        }

        #region DependencyProperty: Status
        public string Status
        {
            get
            {
                return (string)GetValue(StatusProperty);
            }
            set
            {
                SetValue(StatusProperty, value);
            }
        }

        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.Register("Status", typeof(string), typeof(SmartForm),
            new FrameworkPropertyMetadata());
        #endregion

    }
}

I have a UserControl called SmartForm which has a DependencyProperty called Status.

In my Window1.xaml, I have the element <local:SmartForm Status="Ready"/>.

I would think then in the constructor of the SmartForm object, that Status would equal "Ready" but instead it equals null.

Why is then the value of the Status property NULL in the constructor of SmartForm?

If not in the UserControl constructor, when do I have access to the value, then?

Window1.xaml:

<Window x:Class="TestPropertyDefine23282.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestPropertyDefine23282"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <local:SmartForm Status="Ready"/>
    </Grid>
</Window>

SmartForm.xaml:

<UserControl x:Class="TestPropertyDefine23282.SmartForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
        <TextBlock x:Name="TestingMessage"/>
    </Grid>
</UserControl>

SmartForm.xaml.cs:

using System.Windows;
using System.Windows.Controls;

namespace TestPropertyDefine23282
{
    public partial class SmartForm : UserControl
    {
        public SmartForm()
        {
            InitializeComponent();

            TestingMessage.Text = Status; //WHY IS STATUS NOT YET SET HERE?

        }

        #region DependencyProperty: Status
        public string Status
        {
            get
            {
                return (string)GetValue(StatusProperty);
            }
            set
            {
                SetValue(StatusProperty, value);
            }
        }

        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.Register("Status", typeof(string), typeof(SmartForm),
            new FrameworkPropertyMetadata());
        #endregion

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

预谋 2024-07-28 05:36:29
<local:SmartForm Status="Ready"/>

翻译为:

SmartForm f = new SmartForm();
f.Status = Status.Ready;

当调用 setter 时,您将可以访问该值。

<local:SmartForm Status="Ready"/>

Translates to:

SmartForm f = new SmartForm();
f.Status = Status.Ready;

You will have access to that value when the setter is called.

黑凤梨 2024-07-28 05:36:29

您可以将该测试消息设置为:

...
    public static readonly DependencyProperty StatusProperty = 
        DependencyProperty.Register("Status", typeof(string), typeof(SmartForm),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.None,
            new PropertyChangedCallback(OnStatusChanged)));

    public static void OnStatusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        ((SmartForm)d).TestingMessage.Text = e.NewValue.ToString();
    }
...

或设置为:

<UserControl 
x:Class="TestPropertyDefine23282.SmartForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestPropertyDefine23282"
Height="300" Width="300"
>
<Grid>
    <TextBlock
        x:Name="TestingMessage"
        Text="{Binding Path=Status, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:SmartForm}}}"
        />
</Grid>
</UserControl>

You can set that testing message as:

...
    public static readonly DependencyProperty StatusProperty = 
        DependencyProperty.Register("Status", typeof(string), typeof(SmartForm),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.None,
            new PropertyChangedCallback(OnStatusChanged)));

    public static void OnStatusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        ((SmartForm)d).TestingMessage.Text = e.NewValue.ToString();
    }
...

Or as:

<UserControl 
x:Class="TestPropertyDefine23282.SmartForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestPropertyDefine23282"
Height="300" Width="300"
>
<Grid>
    <TextBlock
        x:Name="TestingMessage"
        Text="{Binding Path=Status, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:SmartForm}}}"
        />
</Grid>
</UserControl>
心作怪 2024-07-28 05:36:29

Szymon Rozga 很好地解释了这个问题。 您可以在设置参数之前、构造函数初始化之后检查参数。

一个好的解决方案是使用已加载的事件,如下所示:(

未经测试)

    public SmartForm()
    {
        InitializeComponent();

        Loaded += (sender, args) =>
        {
            TestingMessage.Text = Status; 
        };
    }

Szymon Rozga exlained the problem in a great way. You check the parameter before it is set but after the constructor is initialized.

A good solution is using the loaded event instead like so:

(Untested)

    public SmartForm()
    {
        InitializeComponent();

        Loaded += (sender, args) =>
        {
            TestingMessage.Text = Status; 
        };
    }
缪败 2024-07-28 05:36:29

这是第三种,但为什么你需要这个二传手呢?

<UserControl x:Class="TestPropertyDefine23282.SmartForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Control"
    Height="300" Width="300">
    <Grid>
        <TextBlock Text="{Binding Path=Status, ElementName=Control}" />
    </Grid>
</UserControl>

This is kind of tertiary, but why do you need this setter at all?

<UserControl x:Class="TestPropertyDefine23282.SmartForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Control"
    Height="300" Width="300">
    <Grid>
        <TextBlock Text="{Binding Path=Status, ElementName=Control}" />
    </Grid>
</UserControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文