Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 强制下拉组合框_C#_Wpf - Fatal编程技术网

C# 强制下拉组合框

C# 强制下拉组合框,c#,wpf,C#,Wpf,昨天我遇到一个帖子,我想我找到了一个好的解决办法。这里是链接 我试着跟随这篇文章,由于我是WPF和XAML的新手,我最终遇到了一个奇怪的错误:Type ComboBox\u ForceDropDown初始化失败。ERP_Lite.Views.DesignRelatedCode.ComboBox_Force下拉列表的类型初始值设定项引发异常。 这是我的密码: //ComboBox_ForceDropDown.cs using System; using System.Collections.Gen

昨天我遇到一个帖子,我想我找到了一个好的解决办法。这里是链接

我试着跟随这篇文章,由于我是WPF和XAML的新手,我最终遇到了一个奇怪的错误:
Type ComboBox\u ForceDropDown初始化失败。ERP_Lite.Views.DesignRelatedCode.ComboBox_Force下拉列表的类型初始值设定项引发异常。

这是我的密码:

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

只能在派生自
DependencyObject
的类上定义
DependencyProperty
。您的类是静态的,因此不能从
DependencyObject
派生。可能这就是您出现异常的原因。

您的属性应该附加在
上。更新您的属性声明,如:

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

异常详细信息可能会提供一些提示…当您得到类似的
异常
时,您可以单击弹出窗口
中名为
查看详细信息
的链接。如果这样做,将打开另一个
窗口
,其中包含所有
异常
详细信息。请特别注意
内部异常
。感谢您提供的答案。现在我通过一个实际的例子了解了附加属性的用法。
'ComboBox_ForceDropDown' type must derive from DependencyObject.
public static readonly DependencyProperty OpenDropDownAutomaticallyProperty = 
                           DependencyProperty.RegisterAttached("OpenDropDownAutomatically",
                             typeof(bool),
                             typeof(ComboBox_ForceDropDown),
                             new UIPropertyMetadata(false, onOpenDropDownAutomatically_Changed)
                            );