2009-10-07 6 views
1

J'essaie d'obtenir une meilleure compréhension de ce que les propriétés de dépendance et ce qu'ils sont pas. J'ai construit l'exemple ci-dessous qui permet de modifier les choix d'une combobox en fonction de la façon dont l'utilisateur déplace un curseur .Comment puis-je étendre cet exemple de propriété de dépendance pour recréer une propriété de dépendance DockPanel.Dock = "Top"?

En créant ce j'ai appris que les propriétés de dépendance ont en fait rien à voir avec INotifyPropertyChanged comme il est utilisé dans les propriétés ViewModel, qui a simplifié l'exemple ci-dessous.

Mais maintenant, comment pourrais-je aller de cet exemple ci-dessous pour recréer le type de propriété de dépendance vu dans DockPanel.Dock="Top", par exemple. pour que je puisse activer le genre suivant l'utilisation de XAML:

<local:ExtendedComboBox 
    Margin="5 5 5 0" 
    DataIdCode="{Binding ElementName=TheSource, Path=Value}"> 
    <Image local:ExtendendedComboBox="Left" ... /> 
    <TextBlock local:ExtendendedComboBox="Right" ... /> 
</local:ExtendedComboBox> 

Est-ce possible? Et est-ce le même type d'utilisation des propriétés de dépendance que dans l'exemple plus simple ci-dessous, ou est-ce, comme INotifyPropertyChanged, une autre sorte de technologie de liaison dans WPF?

Voici l'exemple curseur/combobox:

XAML:

<Window x:Class="TestDependency9202.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:TestDependency9202" 
    Title="Window1" Height="300" Width="300"> 
    <StackPanel> 
     <StackPanel 
      Margin="5 5 5 0" 
      Orientation="Horizontal"> 
      <TextBlock Text="Customers" 
         Margin="0 0 3 0"/> 
      <Slider x:Name="TheSource" 
       HorizontalAlignment="Left" 
       Value="0" 
       Width="50" 
       SnapsToDevicePixels="True" 
       Minimum="0" 
       Margin="0 0 3 0" 
       Maximum="1"/> 
      <TextBlock Text="Employees"/> 
     </StackPanel> 
     <local:ExtendedComboBox 
      Margin="5 5 5 0" 
      DataIdCode="{Binding ElementName=TheSource, Path=Value}"/> 
    </StackPanel> 
</Window> 

code-behind:

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

namespace TestDependency9202 
{ 
    public partial class ExtendedComboBox : ComboBox 
    { 
     public static readonly DependencyProperty DataIdCodeProperty = 
      DependencyProperty.Register("DataIdCode", typeof(string), typeof(ExtendedComboBox), 
       new PropertyMetadata(string.Empty, OnDataIdCodePropertyChanged)); 

     private static void OnDataIdCodePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) 
     { 
      ExtendedComboBox extendedComboBox = dependencyObject as ExtendedComboBox; 
      extendedComboBox.OnDataIdCodePropertyChanged2(e); 
     } 

     private void OnDataIdCodePropertyChanged2(DependencyPropertyChangedEventArgs e) 
     { 
      if (DataIdCode == "0") 
      { 
       Items.Clear(); 
       Items.Add("customer1"); 
       Items.Add("customer2"); 
       Items.Add("customer3"); 
      } 
      else if (DataIdCode == "1") 
      { 
       Items.Clear(); 
       Items.Add("employee1"); 
       Items.Add("employee2"); 
       Items.Add("employee3"); 
      } 
      this.SelectedIndex = 0; 
     } 


     public string DataIdCode 
     { 
      get { return GetValue(DataIdCodeProperty).ToString(); } 
      set { SetValue(DataIdCodeProperty, value); } 
     } 

     public ExtendedComboBox() 
     { 
      InitializeComponent(); 
     } 
    } 

} 

Répondre

3

Ce genre de propriété de dépendance est appelée Attached Property. C'est essentiellement une propriété de dépendance utilisée sur un autre objet. Vous utilisez DependencyProperty.RegisterAttached pour les créer et fournissez deux méthodes statiques pour les obtenir et les définir. Voir ci-dessus link.

Questions connexes