简体   繁体   中英

Using Calburn.Micro with the Extended WPF Toolkit?

I am using the IntegerUpDown control from the Extended WPF Toolkit and I am using Caliburn.Micro and PostSharp also. I trying to get it to set the maximum and minimum values for the control based on properties in my ViewModel.

I can get the Minumum or Maximum value to work, but not both. So I am obviously doing something that only allows the last property binding to stick. Here is my AppBootstrapper class:

using Caliburn.Micro;
using System.Windows;
using Xceed.Wpf.Toolkit;

namespace Test {
    public class AppBootstrapper : Bootstrapper<MainViewModel>{

        static AppBootstrapper() {
            var baseBindProperties = ViewModelBinder.BindProperties;

            ConventionManager.AddElementConvention<FrameworkElement>(IntegerUpDown.MinimumProperty, "Minimum", "ValueChanged");
            ViewModelBinder.BindProperties =
                (frameWorkElements, viewModels) => {
                    foreach (var frameworkElement in frameWorkElements) {
                        var propertyName = frameworkElement.Name + "Minimum";
                        var property = viewModels
                                .GetPropertyCaseInsensitive(propertyName);
                        if (property != null) {
                            var convention = ConventionManager
                                .GetElementConvention(typeof(FrameworkElement));
                            ConventionManager.SetBindingWithoutBindingOverwrite(
                                viewModels,
                                propertyName,
                                property,
                                frameworkElement,
                                convention,
                                convention.GetBindableProperty(frameworkElement));
                        }
                    }
                    return baseBindProperties(frameWorkElements, viewModels);
                };

            ConventionManager.AddElementConvention<FrameworkElement>(IntegerUpDown.MaximumProperty, "Maximum", "ValueChanged");
            ViewModelBinder.BindProperties =
                (frameWorkElements, viewModels) => {
                    foreach (var frameworkElement in frameWorkElements) {
                        var propertyName = frameworkElement.Name + "Maximum";
                        var property = viewModels
                                .GetPropertyCaseInsensitive(propertyName);
                        if (property != null) {
                            var convention = ConventionManager
                                .GetElementConvention(typeof(FrameworkElement));
                            ConventionManager.SetBindingWithoutBindingOverwrite(
                                viewModels,
                                propertyName,
                                property,
                                frameworkElement,
                                convention,
                                convention.GetBindableProperty(frameworkElement));
                        }
                    }
                    return baseBindProperties(frameWorkElements, viewModels);
                };
        }
    }
}

In the example above, the Maximum value is set, but not the minimum. If I swap them around, so that the Minimum binding is set last, the Minimum works but the Maximum does not. What am I doing wrong here?

For completeness sake if you want to run this, here is the MainView.xaml:

<Window x:Class="Test.MainView"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox Name="Text"/>
        <xctk:IntegerUpDown Name="Number"/>
        <Button Name="Click" Height="25" Content="Test"/>
    </StackPanel>
</Window>

and the MainViewModel.cs:

using Caliburn.Micro;
using PostSharp.Patterns.Model;
using System;

namespace Test {

    [NotifyPropertyChanged]
    public class MainViewModel : Screen {

        public string Text { get; set; }

        public int Number { get; set; }

        public int NumberMaximum { get; set; }

        public int NumberMinimum { get; set; }

        public MainViewModel()
            : base() {
            this.NumberMinimum = 50;
            this.NumberMaximum = 100;
            this.Number = 75;
        }

        public void Click() {
            Console.WriteLine("Text: '"+this.Text+"'");
            Console.WriteLine("Number: '"+this.Number+"'");
        }

        protected void OnPropertyChanged(string propertyName) {
            NotifyOfPropertyChange(propertyName);
        }
    }
}

What I figured out is that

ConventionManager.GetElementConvention(typeof(FrameworkElement));

was not really returning the correct convention, and instead always returning the last convention that was added. I also think I was setting them up in the wrong place in the static constructor. So I moved it into an overriden Configure method. My AppBootstrapper class now looks like:

using Caliburn.Micro;
using System.Windows;
using Xceed.Wpf.Toolkit;

namespace Test {
    public class AppBootstrapper : Bootstrapper<MainViewModel>{

        protected override void Configure() {
            base.Configure();

            //setup the conventions
            var valueConvention = ConventionManager.AddElementConvention<FrameworkElement>(IntegerUpDown.ValueProperty, "Value", "ValueChanged");
            var maximumConvention = ConventionManager.AddElementConvention<FrameworkElement>(IntegerUpDown.MaximumProperty, "Maximum", "ValueChanged");
            var minimumConvention = ConventionManager.AddElementConvention<FrameworkElement>(IntegerUpDown.MinimumProperty, "Minimum", "ValueChanged");

            //bind the properties
            var baseBindProperties = ViewModelBinder.BindProperties;
            ViewModelBinder.BindProperties =
                (frameWorkElements, viewModels) => {

                    foreach (var frameworkElement in frameWorkElements) {
                        var valuePropertyName = frameworkElement.Name;
                        var valueProperty = viewModels
                                .GetPropertyCaseInsensitive(valuePropertyName);

                        if (valueProperty != null) {
                            ConventionManager.SetBindingWithoutBindingOverwrite(
                                    viewModels,
                                    valuePropertyName,
                                    valueProperty,
                                    frameworkElement,
                                    valueConvention,
                                    valueConvention.GetBindableProperty(frameworkElement));
                        }

                        var maxPropertyName = frameworkElement.Name + "Maximum";
                        var maxProperty = viewModels
                                .GetPropertyCaseInsensitive(maxPropertyName);

                        if (maxProperty != null) {
                            ConventionManager.SetBindingWithoutBindingOverwrite(
                                    viewModels,
                                    maxPropertyName,
                                    maxProperty,
                                    frameworkElement,
                                    maximumConvention,
                                    maximumConvention.GetBindableProperty(frameworkElement));
                        }

                        var minPropertyName = frameworkElement.Name + "Minimum";
                        var minProperty = viewModels
                                .GetPropertyCaseInsensitive(minPropertyName);

                        if (minProperty != null) {
                            ConventionManager.SetBindingWithoutBindingOverwrite(
                                viewModels,
                                minPropertyName,
                                minProperty,
                                frameworkElement,
                                minimumConvention,
                                minimumConvention.GetBindableProperty(frameworkElement));
                        }
                    }

                    return baseBindProperties(frameWorkElements, viewModels);
                };

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM