2009-05-25 57 views
4

我有一个名为SmartForm的用户控件,它具有名为状态的DependencyProperty。如何从代码隐藏构造函数中访问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 

    } 
} 
+2

要回答这个问题:这个值是在装载的情况下可用。 – 2010-03-11 09:01:14

回答

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> 
+0

xaml方法更好,您不必担心任何人在控件之外捕获和处理预览属性更改的事件,并且如果要为多个DP设置此属性,它会更干净。 – 2009-05-25 15:36:21

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

翻译为:

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

您将有机会获得该值时,调用setter时。

+0

就我而言,二传手永远不会被调用。使用Loaded事件为我工作。 – 2017-06-22 20:36:22

-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> 
1

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

一个很好的解决方案是使用加载的事件,而不是像这样:

(未经测试)

public SmartForm() 
    { 
     InitializeComponent(); 

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