【问题标题】:Silverlight: Default value in ComboboxSilverlight:组合框中的默认值
【发布时间】:2011-05-27 08:27:22
【问题描述】:

我想在组合框中显示默认文本。例如,“选择一个人”消息。你能帮帮我吗?

请注意,我正在使用来自域上下文的数据绑定

谢谢!!

【问题讨论】:

    标签: silverlight


    【解决方案1】:

    为了实现这一点,我使用了一个派生的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);
        }
    }
    

    【讨论】:

    • 有没有一种简单的方法可以让用户在做出选择后选择“未选择”状态?
    • @Jordan Just set .SelectedItem = null;
    • 在代码中,我知道。我说的是用户能够将其设置回“未选择”。
    • @Jordan 然后可以使用内置的组合框控件,在 items 集合的开头添加 1 个项目。
    • 那是我试图避免的。你认为这将是一个常见的用例,但我想它不是。我将组合框绑定到对象而不是字符串,所以它让事情变得有点混乱。 sigh 谢谢,我真的很喜欢你的ExtendedComboBox,我一定会用的。
    【解决方案2】:

    这样做:

    theComboBox.SelectedItem  = yourDataItem;
    

    或者,设置选定的索引:

    theComboBox.SelectedIndex = 0;
    

    编辑

    如果绑定了 ItemSource,您希望覆盖组合的 DataContextChanged,然后使用上述行之一来设置索引/选定项。


    但是,如果您不希望选择默认文本,则必须按照this 的方式进行操作。

    【讨论】:

    • 我将下拉菜单绑定到数据上下文:(
    【解决方案3】:

    您可以使用设置值方法在特定位置插入值。 这可能不是最佳选择,但对我有用。

    var array= APPTasks.ListPhysician.OrderBy(e => e.phyFName).ThenBy(e => e.phyLName).ToArray();
    
    array.SetValue((new Physician { phyFName = "Select Attening Md", phyLName = "" }), 0);
    

    只需将您的数据放入变量中并使用 SetValue 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 2012-07-25
      • 2011-11-21
      相关资源
      最近更新 更多