6
A
回答
12
爲了達到這個目的,我使用了派生的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", 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);
}
}
0
只是這樣做:
theComboBox.SelectedItem = yourDataItem;
另外,設置所選指數:
theComboBox.SelectedIndex = 0;
編輯
如果的ItemSource綁定,要覆蓋組合的DataContextChanged
和然後使用上述其中一行設置索引/選定項目。
但是,如果你不想默認的文本可以進行選擇,你會做沿着this線的東西。
+0
我綁定下拉菜單數據上下文:( – 2011-05-29 15:06:37
相關問題
- 1. Python3 - 默認值爲type的默認值?
- 2. java中的默認值
- 3. C++中的GUID默認值
- 4. TurboC中的默認值
- 5. Spring中PropertiesPlaceholderConfigurer的默認值?
- 6. c中bool的默認值#
- 7. CodeIgniter中的set_value()默認值
- 8. C++中bool的默認值
- 9. Rails中create_table的默認值
- 10. iterationcombo框中的默認值
- 11. Hibernate中CascadeType的默認值
- 12. nhibernate中的默認值
- 13. BOOL的默認值
- 14. datetime的默認值
- 15. EditText的默認值
- 16. textarea的默認值
- 17. javadoc的默認值
- 18. 默認值:inputText的
- 19. 默認值 - InnoDB的
- 20. Django的默認值
- 21. DatePicker的默認值
- 22. textarea的默認值
- 23. SqlDataSource的默認值
- 24. OpenERP的默認值
- 25. Ruby:define的默認值?
- 26. Honeycomb的默認值
- 27. 默認值 - 的JavaScript
- 28. 的SelectList默認值
- 29. xmlns的默認值?
- 30. read_only_fields的默認值
非常感謝你,它解決了這個問題 – 2011-05-30 08:03:48
有沒有一種簡單的方法,以允許用戶在選擇完成後選擇「Not Selected」狀態 – Jordan 2013-05-20 20:12:26
@Jordan只需設置'.SelectedItem = null;' – vorrtex 2013-05-21 09:24:52