Binding in WinForms over WPF
От: Namelles.One  
Дата: 26.12.10 18:23
Оценка:
Господа, день добрый. Пишу проект на .NET 3.5, понадобился DateTimePicker, взял оный из Windows Forms.

Разбираюсь с проблемой байндинга к Windows Forms элементам. Нагуглил один интересный код, но, к сожалению, он не работает. Проблема в том, что мне он кажется абсолютно рабочим, видимо не хватает фактических знаний. Помогите, пожалуйста, разобраться, почему не работает как должно?


    public class Proxy : FrameworkElement
    {
        public static readonly DependencyProperty InProperty;
        public static readonly DependencyProperty OutProperty;

        private delegate void Operation();

        public Proxy()
        {
            Visibility = Visibility.Collapsed;
        }

        static Proxy()
        {
            FrameworkPropertyMetadata inMetadata = new FrameworkPropertyMetadata(
                delegate(DependencyObject p, DependencyPropertyChangedEventArgs args)
                {
                    if (null != BindingOperations.GetBinding(p, OutProperty))
                        ((Proxy) p).Out = args.NewValue;
                }) {BindsTwoWayByDefault = false, DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged};

            InProperty = DependencyProperty.Register("In",
                typeof(object),
                typeof(Proxy),
                inMetadata);

            FrameworkPropertyMetadata outMetadata = new FrameworkPropertyMetadata(
                delegate(DependencyObject p, DependencyPropertyChangedEventArgs args)
                {
                    ValueSource source = DependencyPropertyHelper.GetValueSource(p, args.Property);

                    if (source.BaseValueSource != BaseValueSource.Local)
                    {
                        Proxy proxy = p as Proxy;
                        if (proxy != null)
                        {
                            object expected = proxy.In;
                            if (!ReferenceEquals(args.NewValue, expected))
                            {
                                Dispatcher.CurrentDispatcher.BeginInvoke(
                                    DispatcherPriority.DataBind, new Operation(delegate
                                                                                   {
                                                                                       proxy.Out = proxy.In;
                                                                                   }));
                            }
                        }
                    }
                }) {BindsTwoWayByDefault = true, DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged};

            OutProperty = DependencyProperty.Register("Out",
                typeof(object),
                typeof(Proxy),
                outMetadata);
        }

        public object In
        {
            get { return GetValue(InProperty); }
            set { SetValue(InProperty, value); }
        }

        public object Out
        {
            get { return GetValue(OutProperty); }
            set { SetValue(OutProperty, value); }
        }
    }


Пример вызова такой:


<help:Proxy In="{Binding Path=CardOrder.MainInfo.CreationDate}" Out="{Binding ElementName=CreatedDate, Path=Value}" />
<WindowsFormsHost Name="windowsFormsHost1" MinWidth="120">
    <WinForms:DateTimePicker x:Name="CreatedDate"/>
</WindowsFormsHost>


Вообще, код интересный, в случае его допиливания — поможет решить много проблем и не мне одному.

27.12.10 06:14: Перенесено из '.NET'
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.