2013-10-22 157 views
0

昨天我遇到了一個帖子,我想我有一個很好的解決方案。這裏是http://www.blogs.intuidev.com/post/2011/01/02/combobox_autoopendropdown_attachedbehavior.aspx強制組合框下拉

我曾試圖按照那個帖子的鏈接,因爲我是一個新手,WPF和XAML我結束了一個奇怪的錯誤:Type ComboBox_ForceDropDown initialization failed. The type initializer for ERP_Lite.Views.DesignRelatedCode.ComboBox_ForceDropDown threw an exception.

這裏是我的代碼:

//ComboBox_ForceDropDown.cs 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 

namespace ERP_Lite.Views.DesignRelatedCode 
{ 
    public static class ComboBox_ForceDropDown 
    { 

     public static readonly DependencyProperty OpenDropDownAutomaticallyProperty = DependencyProperty.Register 
                         (
                          "OpenDropDownAutomatically", 
                          typeof(bool), 
                          typeof(ComboBox_ForceDropDown), 
                          new UIPropertyMetadata(false, onOpenDropDownAutomatically_Changed) 
                         ); 

     public static bool GetOpenDropDownAutomatically(ComboBox cbo) 
     { 
      return (bool)cbo.GetValue(OpenDropDownAutomaticallyProperty); 
     } 
     public static void SetOpenDropDownAutomatically(ComboBox cbo, bool value) 
     { 
      cbo.SetValue(OpenDropDownAutomaticallyProperty, value); 
     } 

     /// <summary> 
     /// Fired when the assignment of the behavior changes (IOW, is being turned on or off). 
     /// </summary> 
     private static void onOpenDropDownAutomatically_Changed(DependencyObject doSource, DependencyPropertyChangedEventArgs e) 
     { 
      //The ComboBox that is the target of the assignment 
      ComboBox cbo = doSource as ComboBox; 
      if (cbo == null) 
       return; 

      //Just to be safe ... 
      if (e.NewValue is bool == false) 
       return; 

      if ((bool)e.NewValue) 
      { 
       //Attach 
       cbo.GotFocus += cbo_GotFocus; 
       cbo.LostFocus += cbo_LostFocus; 
      } 
      else 
      { 
       //Detach 
       cbo.GotFocus -= cbo_GotFocus; 
       cbo.LostFocus -= cbo_LostFocus; 
      } 

     } 

     private static void cbo_GotFocus(object sender, RoutedEventArgs e) 
     { 
      //Open the DropDown/popup as soon as the control is focused 
      ((ComboBox)sender).IsDropDownOpen = true; 
     } 

     private static void cbo_LostFocus(object sender, RoutedEventArgs e) 
     { 
      ((ComboBox)sender).IsDropDownOpen = false; 
     } 
    } 
} 

而XAML文件

//App.xaml 
<Application 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:comboFDD="clr-namespace:ERP_Lite.Views.DesignRelatedCode" 
    x:Class="ERP_Lite.App" StartupUri="Views/MainWindow.xaml"> 
    <Application.Resources> 
     <!-- Resources scoped at the Application level should be defined here. --> 
     <Style TargetType="{x:Type ComboBox}"> 
     <Setter Property="StaysOpenOnEdit" Value="True" /> 
     <Setter Property="comboFDD:ComboBox_ForceDropDown.OpenDropDownAutomatically" Value="True"/> <!--I get error on this line--> 
     </Style> 
    </Application.Resources> 
</Application> 

這裏是解決方案管理器的圖像:

enter image description here

更新:內異常詳細信息如下:

'ComboBox_ForceDropDown' type must derive from DependencyObject. 
+0

異常的詳細信息可能會提供一些線索...... – makc

+0

當你得到一個'Exception'這樣,您可以點擊彈出的鏈接'Window'稱爲'查看Details' 。如果你這樣做了,另一個'Window'將打開所有'Exception'的細節。要特別注意'內部例外'。 – Sheridan

回答

2

您的物業應該是AttachedDependancyProperty。更新你的財產申報,如:

public static readonly DependencyProperty OpenDropDownAutomaticallyProperty = 
          DependencyProperty.RegisterAttached("OpenDropDownAutomatically", 
          typeof(bool), 
          typeof(ComboBox_ForceDropDown), 
          new UIPropertyMetadata(false, onOpenDropDownAutomatically_Changed) 
          ); 
+0

感謝您提供的答案。現在我明白了使用附加屬性和一個實際的例子。 – Khushi

0

DependencyProperty只能在從DependencyObject派生的類來定義。你的課是靜態的,因此不能從DependencyObject派生。可能這就是你例外的原因。