在IsEditable=true和ReadOnly=true的情况下,是否可以使RadComboBox的整个文本区域可点击?
我只想设置IsEditable=false,但不幸的是,我需要它是可编辑的,以便在选择某个内容时显示自定义文本(我设置了它,以便可以选择多个内容并显示所选项目的列表)。如果禁用IsEditable,则会丢失.Text属性,并且无法设置自定义文本。
我的两个最佳选择是:
1) 以某种方式应用一种样式,使整个文本栏可点击,而不仅仅是箭头
2) 当IsEditable设置为false时,以某种方式应用自定义文本显示。
不幸的是,我也不知道该怎么做,所以任何帮助都会很好。感谢
编辑:这将是理想的,除了我们使用的是Silverlight而不是ASP.nethttp://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/checkboxes/defaultcs.aspx
这可能更现实,只是为了让文本区域可以点击,从而打开下拉菜单。就像右边的组合框一样,减去可以键入
http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/comboboxvsdropdownlist/defaultcs.aspx
我能想到几种不同优雅的解决方案。这里有一个可能适合缩小箭头切换按钮和文本输入区域之间的剩余间隙。现在我想一想…也许你可以摆脱OpenDropDownOnFocus
属性带来的相当难闻和脆弱的副作用(只要点击不改变焦点所有者,它就会崩溃)。
在RadComboBox中注册MouseLeftButtonDown
单击处理程序,您可以选择获取所有事件,而不仅仅是未处理的事件。然后我们可以从那里切换DropDown。但我们不想干扰箭头切换按钮,因此我们从鼠标点击的来源进行检查。
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;
}
}
我最终找到了一个其他人在这里实现的多选择器组合框:
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>
在我们自己的自定义多选组合框的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,所以当我们设置RadComboBoxText时,TextBlock会更新自己。
这对我们来说是最有效的方法,因为它只需要最少的代码更改,而且不需要任何结构更改,因为我们已经准备好了所有的代码来复选框和设置自定义文本。
希望这能帮助到别人,祝你好运!