Silverlight:Combobox中的默认值

时间:2011-05-27 08:27:23

标签: silverlight

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

请注意我正在使用domaincontext的数据绑定

谢谢!

3 个答案:

答案 0 :(得分:12)

为了实现这一点,我使用了派生的ExtendedComboBox类,它扩展了内置的ComboBox类。您可以在my blog post或更低版本中找到此课程的源代码。

将此类添加到项目后,可以使用此XAML代码显示默认值:

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

此外,这是具有此控件的test page。我认为第二个组合框就是你所需要的。 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)

这样做:

theComboBox.SelectedItem  = yourDataItem;

或者,设置所选索引:

theComboBox.SelectedIndex = 0;

修改

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


但是,如果您不希望选择默认文本,则必须执行this行。

答案 2 :(得分:0)

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

var array= APPTasks.ListPhysician.OrderBy(e => e.phyFName).ThenBy(e => e.phyLName).ToArray();

array.SetValue((new Physician { phyFName = "Select Attening Md", phyLName = "" }), 0);

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