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>

这是解决方案资源管理器的图像:

在此处输入图像描述

更新:内部异常详细信息如下:

'ComboBox_ForceDropDown' type must derive from DependencyObject.
4

2 回答 2

2

你的财产应该是Attached DependancyProperty. 更新您的财产声明,如:

public static readonly DependencyProperty OpenDropDownAutomaticallyProperty = 
                           DependencyProperty.RegisterAttached("OpenDropDownAutomatically",
                             typeof(bool),
                             typeof(ComboBox_ForceDropDown),
                             new UIPropertyMetadata(false, onOpenDropDownAutomatically_Changed)
                            );
于 2013-10-22T14:58:05.627 回答
0

ADependencyProperty只能在派生自 的类上定义DependencyObject。您的类是静态的,因此不能从DependencyObject. 可能这就是你例外的原因。

于 2013-10-22T14:38:05.917 回答