silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Silverlight:Combobox中的默认值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我想在组合框中显示默认文本.例如,“选择一个人”消息.你能帮帮我吗? 请注意我正在使用domaincontext的数据绑定 谢谢 !! 为此,我使用了一个派生的ExtendedComboBox类,它扩展了内置的ComboBox类.您可以在 my blog post或更低版本中找到此类的源代码. 将此类添加到项目后,可以使用此XAML代码显示默认值: <local:ExtendedComboBox
我想在组合框中显示认文本.例如,“选择一个人”消息.你能帮帮我吗?

请注意我正在使用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中的默认值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。