Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
XAML binding
Message
De
23/07/2009 13:22:34
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Titre:
XAML binding
Versions des environnements
Environment:
C# 3.0
Divers
Thread ID:
01414001
Message ID:
01414001
Vues:
78
Hi,
When is a XAML defined binding instantiated?
Here's a cutdown version of a class I have:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace LLControls.UserControls
{
    public partial class Test : UserControl
    {
        private static readonly DependencyProperty TargetControlProperty;

        static Test()
        {
            FrameworkPropertyMetadata fm = new FrameworkPropertyMetadata();
            fm.PropertyChangedCallback = TargetChanged;
            TargetControlProperty = DependencyProperty.Register("TargetControl", typeof(ILLObject), typeof(Test), fm);
        }

        public Test()
        {
            InitializeComponent();
            if (!BindingOperations.IsDataBound(this, TargetControlProperty))  //Always false at this point
                CanvasHolder.CurrentElementChanged += new CanvasHolder.CurrentElementChangedEventHandler(CurrentElementChanged);
        }

        public ILLObject TargetControl
        {
            get { return (ILLObject)GetValue(TargetControlProperty); }
            set { SetValue(TargetControlProperty, value); }
        }

        private static void TargetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Test)d).CurrentElementChanged((ILLObject)e.NewValue);
            return;
        }

        void CurrentElementChanged(ILLObject ilo) { }
    }
}
The idea here is that normally we handle the CanvasHolder.CurrentElementChanged event. However the XAML designer may wish to use this class by binding to the TargetControlProperty in XAML. When this happens I don't want to have the event wired up but to just use the XAML binding instead. The problem with the above code is that the .IsDataBound() in the initializer always returns false so I can't make the decision there. The only other option I can see would be to always wire up the handler in the initializer and then remove it in the TargetChanged method if it is called but this feels messy (to say the least).
Any other suggestions, TIA,
Viv
Répondre
Fil
Voir

Click here to load this message in the networking platform