2009-05-25 6 views
4

J'ai un UserControl appelé SmartForm qui a un DependencyProperty appelé Statut. Dans mon Window1.xaml, j'ai l'élément <local:SmartForm Status="Ready"/>.Comment puis-je accéder aux valeurs DependencyProperty à partir de mon constructeur code-behind?

Je penserais alors dans le constructeur de l'objet SmartForm, que Status serait égal à "Ready" mais à la place il est égal à null.

Pourquoi alors la valeur de la propriété Status NULL dans le constructeur de SmartForm?

Si ce n'est pas dans le constructeur UserControl, quand puis-je avoir accès à la valeur, alors?

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

Pour répondre à la question: La valeur est disponible en cas onLoaded. –

Répondre

3

Yo u peut mettre ce message de test comme:

... 
    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(); 
    } 
... 

Ou comme:

<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

La méthode xaml est meilleure, vous n'aurez pas à vous inquiéter si quelqu'un attrape et manipule l'événement modifié de propriété de prévisualisation en dehors du contrôle et il est plus propre si vous configurez ceci pour plusieurs DP. –

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

traduit:

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

Vous aurez accès à cette valeur lorsque le poseur est appelé .

+0

Dans mon cas, le setter n'a jamais été appelé. L'utilisation de l'événement Loaded a fonctionné pour moi. –

-1

C'est un peu tertiaire, mais pourquoi avez-vous besoin de ce setter? Szymon Rozga a expliqué le problème d'une manière remarquable.

<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

Vous vérifiez le paramètre avant qu'il ne soit défini mais après l'initialisation du constructeur.

Une bonne solution utilise l'événement chargé à la place comme ceci:

(non testé)

public SmartForm() 
    { 
     InitializeComponent(); 

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