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

时间:2009-05-25 13:26:43

标签: wpf xaml dependency-properties

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

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

我认为在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

    }
}

4 个答案:

答案 0 :(得分: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>

答案 1 :(得分:3)

<local:SmartForm Status="Ready"/>

转换为:

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

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

答案 2 :(得分:1)

Szymon Rozga以一种很好的方式解决了这个问题。在设置参数之前检查参数,但是在初始化构造函数之后。

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

(未测试的)

    public SmartForm()
    {
        InitializeComponent();

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

答案 3 :(得分:-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>