簡體   English   中英

強制組合框下拉

[英]Force a combobox to dropdown

昨天我遇到了一個帖子,我認為我得到了一個很好的解決方案。 這是鏈接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. 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.

您的財產應該是Attached DependancyProperty 更新您的屬性聲明,如:

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

DependencyProperty只能在派生自DependencyObject類上定義。 您的類是靜態的,因此無法從DependencyObject派生。 可能這是你例外的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM