2013-06-25 2 views
1

확장 WPF 툴킷에서 IntegerUpDown 컨트롤을 사용하고 있으며 Caliburn.Micro 및 PostSharp도 사용하고 있습니다. 내 ViewModel의 속성을 기반으로 컨트롤의 최대 값과 최소값을 설정하려고했습니다.Extended WPF Toolkit과 함께 Calburn.Micro 사용?

작동하려면 최소 또는 최대 값을 얻을 수 있지만 둘 다 사용할 수는 없습니다. 그래서 나는 분명히 마지막 속성 바인딩 만 허용하는 무언가를하고 있습니다. 여기 내 AppBootstrapper 클래스 : 위의 예에서

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

, 최대 값은 최소값을 설정할 수 있지만되지 않습니다. 최소 바인딩이 마지막으로 설정되도록 최소로 바꾸지 만 최소는 작동하지만 최대는 작동하지 않습니다. 여기서 내가 뭘 잘못하고 있니?

<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> 

과 MainViewModel.cs :

완성도를 위해서

이를 실행하려는 경우, 여기에 MainView.xaml입니다 내가 알아 낸 무엇

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

답변

2

입니다
ConventionManager.GetElementConvention(typeof(FrameworkElement)); 

실제로 올바른 규칙을 반환하지 않고 대신 항상 마지막 규칙을 반환했습니다. t가 추가되었습니다. 또한 정적 생성자의 잘못된 위치에 설정했다고 생각합니다. 그래서 이것을 우선 Configure 메소드로 옮겼습니다. 내 AppBootstrapper 클래스는 다음과 같이 보입니다 :

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

     } 
    } 
}