5

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

在我的 Window1.xaml 中,我有元素<local:SmartForm Status="Ready"/>.

然后我会认为在 SmartForm 对象的构造函数中, Status 将等于 "Ready" 但它等于null

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

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

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

    }
}
4

4 回答 4

4
<local:SmartForm Status="Ready"/>

转换为:

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

调用 setter 时,您将有权访问该值。

于 2009-05-25T14:57:17.303 回答
3

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

...
    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>
于 2009-05-25T14:30:21.227 回答
1

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

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

(未经测试)

    public SmartForm()
    {
        InitializeComponent();

        Loaded += (sender, args) =>
        {
            TestingMessage.Text = Status; 
        };
    }
于 2016-12-16T15:45:50.167 回答
-1

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

<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>
于 2009-05-26T06:37:05.757 回答