【问题标题】:Silverlight RadComboBox make whole texbox area clickableSilverlight RadComboBox 使整个 texbox 区域可点击
【发布时间】:2014-10-06 22:29:29
【问题描述】:

是否可以在 IsEditable=true 和 ReadOnly=True 时使 RadComboBox 的整个文本区域可点击?

我只会设置 IsEditable = false 但不幸的是,我需要它是可编辑的,以便在选择某些内容时显示自定义文本(我已设置它以便可以选择多个内容并显示所选项目的列表)。如果我禁用 IsEditable,那么我会丢失 .Text 属性并且无法设置自定义文本。

我最好的两个选择是:
1) 以某种方式应用一种样式,使整个文本栏可点击,而不仅仅是箭头
2) 当 IsEditable 设置为 false 时,以某种方式应用自定义文本显示。

不幸的是,我也不知道该怎么做,所以任何帮助都会很好。谢谢

编辑:这将是理想的,除了我们使用 Silverlight 而不是 ASP.net http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/checkboxes/defaultcs.aspx

这可能更现实,只是为了以某种方式使文本区域可点击,以便打开下拉菜单。就像右边的 ComboBox 一样,减去能够打字。
http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/comboboxvsdropdownlist/defaultcs.aspx

【问题讨论】:

    标签: c# xaml silverlight telerik radcombobox


    【解决方案1】:

    我能想到几种解决方案,它们的优雅程度各不相同。这是一个可能适合缩小箭头切换按钮和文本输入区域之间剩余间隙的方法。现在我想起来了......也许你可以用OpenDropDownOnFocus属性摆脱那种相当臭和脆弱的副作用(只要点击不改变焦点所有者,它就会中断)。

    向 RadComboBox 注册一个MouseLeftButtonDown 点击处理程序,您可以选择获取所有事件,而不仅仅是未处理的事件。然后我们可以从那里切换 DropDown。但我们不想干扰 Arrow-ToggleButton,因此我们检查鼠标点击的来源。

    public class MyView : UserControl
    {
        public MyView()
        {
            InitializeComponent();
            MouseButtonEventHandler handler = OnComboBoxClicked;
            radComboBox.AddHandler( UIElement.MouseLeftButtonDownEvent, handler,
                handledEventsToo: true );
        }
    
        private void OnComboBoxClicked( object sender, MouseButtonEventArgs args )
        {
            if (!args.Handled ||
                !args.IsRoutedEventFromToggleButton(
                    togglebuttonAncestorToStopTheSearch: (UIElement) sender))
            {
                ToggleDropDown();
            }
        }
    }
    

    以及更容易使用的扩展方法:

    public static class ControlExtensions
    {
        public static bool IsRoutedEventFromToggleButton(
            this RoutedEventArgs args,
            UIElement togglebuttonAncestorToStopTheSearch )
        {
            ToggleButton toggleButton = ((UIElement) args.OriginalSource)
                .GetAncestor<ToggleButton>( togglebuttonAncestorToStopTheSearch );
            return toggleButton != null;
        }
    
        public static TAncestor GetAncestor<TAncestor>(
            this DependencyObject subElement,
            UIElement potentialAncestorToStopTheSearch )
            where TAncestor : DependencyObject
        {
            DependencyObject parent;
            for (DependencyObject subControl = subElement; subControl != null;
                 subControl = parent)
            {
                if (subControl is TAncestor) return (TAncestor) subControl;
    
                if (object.ReferenceEquals( subControl,
                    potentialAncestorToStopTheSearch )) return null;
    
    
                parent = VisualTreeHelper.GetParent( subControl );
                if (parent == null)
                {
                    FrameworkElement element = subControl as FrameworkElement;
                    if (element != null)
                    {
                        parent = element.Parent;
                    }
                }
            }
            return null;
        }
    }
    

    【讨论】:

    • 我实现了这个,但它仍然不允许在启用 IsEditable 的组合框中单击文本和下拉箭头之间的区域。不过谢谢。我找到了一个完整的解决方案,我很快就会在这里发布
    【解决方案2】:

    我最终找到了一个其他人在这里实现的多选组合框:

    http://www.telerik.com/support/code-library/a-multiselect-combobox

    我不需要整个组合框本身,因为我们已经实现了一个,所以我只是查看了当组合框 IsEditable 设置为 false 时该人如何显示自定义消息。

    在查看该代码一段时间并了解如何使其适合我之后,我将

    <ucControls:RadComboBox.SelectionBoxTemplate> <DataTemplate> <TextBlock Text="{Binding Text,ElementName=RadCombo}" /> </DataTemplate> </ucControls:RadComboBox.SelectionBoxTemplate>

    在我们自己的自定义 MultiSelectComboBox 的 XAML 中。 (RadCombo 是我希望文本链接到的特定控件的名称)

    <ucControls:RadComboBox
        x:Name="RadCombo"
        Text=""
        ........
    

    <ucControls:RadComboBox.SelectionBoxTemplate> <DataTemplate> <TextBlock Text="{Binding Text,ElementName=RadCombo}" /> </DataTemplate> </ucControls:RadComboBox.SelectionBoxTemplate>

        .......
    </ucControls:RadComboBox>
    

    使用内置的SelectionBoxTemplate,这基本上只是添加了一个TextBlock覆盖,并且内容绑定到RadComboBox自己的Text,所以当我们设置RadComboBox的Text时,TextBlock会自行更新。

    这对我们来说是最有效的方法,因为它只需要最少的代码更改,并且不需要结构更改,因为我们已经准备好用于复选框和设置自定义文本的所有代码。

    希望这对某人有所帮助,祝你好运!

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 1970-01-01
      相关资源
      最近更新 更多