
请注意我正在使用domaincontext的数据绑定
谢谢 !!
解决方法 为此,我使用了一个派生的ExtendedComboBox类,它扩展了内置的ComboBox类.您可以在 my blog post或更低版本中找到此类的源代码.将此类添加到项目后,可以使用此XAML代码显示默认值:
<local:ExtendedComboBox ItemsSource="{Binding ...Whatever...}" NotSelectedText="Select item..." /> 此外,这是带有此控件的test page.我认为第二个组合框就是你所需要的.
这个类的完整代码:
[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",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:Combobox中的默认值全部内容,希望文章能够帮你解决Silverlight:Combobox中的默认值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)