2011-05-27 18 views
6

में डिफ़ॉल्ट मान मैं combobox में एक डिफ़ॉल्ट पाठ प्रदर्शित करना चाहता हूं। उदाहरण के लिए, "एक व्यक्ति चुनें" संदेश। क्या तुमसे मुझे मदद मिल सकती है।सिल्वरलाइट: Combobox

कृपया ध्यान दें कि मैं से domaincontext

डेटा बाइंडिंग का उपयोग कर रहा धन्यवाद !!

उत्तर

12

इसे प्राप्त करने के लिए, मैंने व्युत्पन्न ExtendedComboBox कक्षा का उपयोग किया जो अंतर्निहित ComboBox वर्ग का विस्तार करता है। आप my blog post या उससे नीचे इस कक्षा का स्रोत कोड पा सकते हैं।

आप अपने प्रोजेक्ट के लिए इस वर्ग को जोड़ने के बाद, आप एक डिफ़ॉल्ट मान प्रदर्शित करने के लिए इस XAML कोड का उपयोग कर सकते हैं:

<local:ExtendedComboBox ItemsSource="{Binding ...Whatever...}" NotSelectedText="Select item..." /> 

इसके अलावा, यहां इस पर नियंत्रण के साथ test page है। मुझे लगता है कि दूसरा combobox है कि आपको क्या चाहिए। example of the extended ComboBox

इस वर्ग के पूर्ण कोड:

[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); 
    } 
} 
+1

आपको बहुत बहुत धन्यवाद, यह समस्या –

+0

हल करने के लिए ड्रॉप-डाउन मेनू बाध्यकारी हूँ वहाँ एक आसान तरीका उपयोगकर्ता अनुमति देने के लिए है चयन के बाद "चयनित नहीं" स्थिति का चयन करने के लिए पहले से ही बनाया जा चुका है? – Jordan

+0

@ जोर्डन बस सेट करें। चयनित Item = null; ' – vorrtex

0

बस इस कार्य करें:

theComboBox.SelectedItem = yourDataItem; 

वैकल्पिक रूप से, चयनित सूचकांक सेट:

theComboBox.SelectedIndex = 0; 

संपादित

ItemSource ही है, तो आप ओवरराइड करना चाहते कॉम्बो के DataContextChanged और फिर इंडेक्स/चयनित आइटम सेट करने के लिए उपर्युक्त पंक्तियों में से एक का उपयोग करें।


हालांकि, अगर आप डिफ़ॉल्ट पाठ चयन होने के लिए नहीं करना चाहते हैं, तो आप this की तर्ज पर कुछ करने के लिए होगा।

+0

मैं एक डेटा संदर्भ :( –