Silverlight:组合框中的默认值 [英] Silverlight: Default value in Combobox
问题描述
我想在组合框中显示默认文本.例如,选择一个人"消息.你能帮我一下吗.
I would like to display a default text in the combobox. For example, "Choose a person" message. could you please help me out.
请注意,我正在使用域上下文中的数据绑定
Please note that I am using databinding from domaincontext
谢谢!!
推荐答案
为了实现这一点,我使用了一个派生的 ExtendedComboBox
类,它扩展了内置的 ComboBox
类.你可以在我的博文 或以下.
To achieve this, I used a derived ExtendedComboBox
class which extends the built-in ComboBox
class. You can find the source code of this class in my blog post or below.
将此类添加到您的项目后,您可以使用此 XAML 代码来显示默认值:
After you add this class to your project, you can use this XAML code to display a default value:
<local:ExtendedComboBox ItemsSource="{Binding ...Whatever...}" NotSelectedText="Select item..." />
此外,这是测试页面这个控制.我认为第二个组合框正是您所需要的.
Also, here is the test page with this control. I think that the second combobox is that what you need.
该类的完整代码:
[TemplateVisualState(Name = ExtendedComboBox.StateNormal, GroupName = ExtendedComboBox.GroupItemsSource)]
[TemplateVisualState(Name = ExtendedComboBox.StateNotSelected, GroupName = ExtendedComboBox.GroupItemsSource)]
[TemplateVisualState(Name = ExtendedComboBox.StateEmpty, GroupName = ExtendedComboBox.GroupItemsSource)]
public class ExtendedComboBox : ComboBox
{
public const string GroupItemsSource = "ItemsSourceStates";
public const string StateNormal = "Normal";
public const string StateNotSelected = "NotSelected";
public const string StateEmpty = "Empty";
private ContentPresenter selectedContent;
public ExtendedComboBox()
{
this.DefaultStyleKey = typeof(ComboBox);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.selectedContent = this.GetTemplateChild("ContentPresenter") as ContentPresenter;
// This event can change the NotSelected state
this.SelectionChanged += (s, e) => this.SetTextIfEmpty();
// Set a state at start
this.SetTextIfEmpty();
}
// This method can change the Empty state
protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
base.OnItemsChanged(e);
this.SetTextIfEmpty();
}
/// <summary>
/// Text if the SelectedItem property is null.
/// </summary>
public string NotSelectedText
{
get { return (string)GetValue(NotSelectedTextProperty); }
set { SetValue(NotSelectedTextProperty, value); }
}
public static readonly DependencyProperty NotSelectedTextProperty =
DependencyProperty.Register("NotSelectedText", typeof(string), typeof(ExtendedComboBox), new PropertyMetadata(" "));
/// <summary>
/// Text if there are no items in the ComboBox at all.
/// </summary>
public string EmptyText
{
get { return (string)GetValue(EmptyTextProperty); }
set { SetValue(EmptyTextProperty, value); }
}
public static readonly DependencyProperty EmptyTextProperty =
DependencyProperty.Register("EmptyText", typeof(string), typeof(ExtendedComboBox), new PropertyMetadata(null));
/// <summary>
/// Changes the state of this control and updates the displayed text.
/// </summary>
protected void SetTextIfEmpty()
{
if (this.selectedContent == null || !(this.selectedContent.Content is TextBlock))
return;
var text = this.selectedContent.Content as TextBlock;
if (this.SelectedItem == null && this.Items != null && this.Items.Count > 0)
{
text.Text = this.NotSelectedText;
VisualStateManager.GoToState(this, ExtendedComboBox.StateNotSelected, true);
}
else if (this.SelectedItem == null)
{
text.Text = this.EmptyText ?? this.NotSelectedText;
VisualStateManager.GoToState(this, ExtendedComboBox.StateEmpty, true);
}
else VisualStateManager.GoToState(this, ExtendedComboBox.StateNormal, true);
}
}
这篇关于Silverlight:组合框中的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!