Silverlight:组合框中的默认值

2023-11-27

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

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

谢谢 !!


为了实现这一点,我使用了派生的ExtendedComboBox扩展内置类ComboBox班级。您可以在以下位置找到该类的源代码我的博文或以下。

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

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

Also, here is the test page with this control. I think that the second combobox is that what you need. 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);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Silverlight:组合框中的默认值 的相关文章

随机推荐

  • Azure 上的 Django 应用程序未获取静态文件

    在 Azure Web 应用程序上获取了我的 Django 项目 但是当我在 SSH 终端上调用时 Python 管理 py 收集静态 它说复制了 252 个文件 但我的静态文件在我的模板上不可见 并且 wwwroot 中的静态文件夹是空的
  • 删除所有文件和文件夹但排除子文件夹

    我有一个文件夹 需要删除除一小部分文件和文件夹之外的所有文件和文件夹 我已经可以排除文件列表 但没有找到排除文件夹及其内容的方法 这是文件夹结构 C temp C temp somefile txt C temp someotherfile
  • 在字符串内包含引号?

    我正在尝试将引号包含在字符串中以添加到文本框 我正在使用此代码 t AppendText Dim Choice count As String Your New Name is pt1 pt2 vbNewLine 但它不起作用 我希望它像这
  • 从大型数据集中随机采样

    我从一个大型数据库中提取了研究人群 为了进行比较 我想选择一个具有相似特征的对照组 我想要匹配的两个标准是年龄和性别 为我提供用于匹配目的的数字的查询是 select sex age 10 as decades COUNT as count
  • URL/子域重写 (htaccess)

    假设我有以下文件 http www example com images folder image jpg 我想把它放在 http s1 example com folder image jpg 我如何进行 htaccess 重写以将其指向
  • GAE交易失败和幂等性

    Google App Engine 文档包含以下段落 注意 如果您的应用程序在提交时收到异常 交易失败并不总是意味着交易失败 你 可以接收DatastoreTimeoutException ConcurrentModificationExc
  • Linq2SQL 处理具有唯一约束的表上的插入/删除

    我有一个如下所示的表 TABLE Foo Guid Id PK int A FK int B FK int C FK 以及对 A B 和 C 的唯一约束 现在举例来说 您插入带有新 PK 的行 其中 A 1 B 1 C 1 SubmitCh
  • 在单个谱系中变基多个分支的最简单方法

    我有两个分支位于上游 主控之上 一个分支以另一个为祖先 因此它们形成一条线 U1 upstream master A B fixes C D features 随后 upstream master 继续前进 U1 U2 upstream m
  • 使用 Flex 编写可重入词法分析器

    我是弯曲的新手 我正在尝试使用 Flex 编写一个简单的可重入词法分析器 扫描器 词法分析器定义如下 我遇到编译错误 如下所示 yyg 问题 可重入 l Definitions digit 0 9 letter a zA Z alphanu
  • 没有打印语句就不会执行代码[重复]

    这个问题在这里已经有答案了 我一直在制作一个倒计时程序 我想出了这个 package main import java awt FlowLayout import java awt event ActionEvent import java
  • 无法在 Azure DevOps 管道中激活 conda

    在 conda 构建的 python 项目上测试 azure devops 管道 jobs job pre build setup displayName Pre Build Setup pool vmImage ubuntu 18 04
  • 析构函数有符合标准的名称吗?

    根据标准 类析构函数是否有迂腐意义上的名称 回想一下 构造函数明确没有名称 12 1 1 构造函数没有名字 A 使用特殊声明符语法 可选序列 后面是函数说明符 7 1 2 通过构造函数的类名 随后使用参数列表 声明或定义构造函数 在这样的声
  • 如何拦截粘贴到 NSTextView 中以删除不支持的格式?

    我正在尝试创建一个简单的基于 NSTextView 的窗口 以进行简单的所见即所得编辑 但是 我只想允许某些类型的格式 例如粗体 斜体 下划线和单个标题类型 但没有颜色或不同的字体 问题是 如果我只是使用 NSTextView 有人可以在另
  • iOS 推送通知 - 更新徽章而不发出警报?

    有没有办法在不显示警报或打开应用程序的情况下更新徽章中的数字 我正在编写一个应用程序 它应该始终在图标徽章中显示当前未读消息的数量 但我想这样做而不向用户显示任何警报 我正在为 iOS 5 0 进行开发 编辑 为了更清楚 我正在询问一种方法
  • Java String.replaceAll 正则表达式

    使用 java 删除输入字符串 如 MY CORP My Name 的 MY CORP 部分的正则表达式是什么String replaceAll方法 这样我只能获取 My Name 部分 I tried public static Stri
  • OpenCV:如何从以太网摄像头捕获帧

    我之前对 USB 网络摄像头进行了编程 其唯一目的是从摄像头获取实时帧并显示在窗口中 我使用 cvCaptureFromCAM 来实现此目的 它对于 USB 摄像头效果很好 请参见下面的代码 我想知道如何从千兆以太网摄像头捕获帧 我想我需要
  • C++ 中的重写删除应如何表现?

    我遇到的问题是 据我所知 删除运算符应该是静态函数 但有时编译器 VC 似乎将其视为动态函数 Given class Base public void operator new size t size allocate from custo
  • PHP:使用自动加载器时如何获取所有类

    我正在使用 Composer 生成自动加载器 autoload psr 4 SomeNamespace src SomeDir 我需要创建实现特定接口的所有类的实例 这相当容易但是 当不使用自动加载器时get declared classe
  • Jquery 使用数组中的数据创建选择标签

    我需要访问数据库并更新选择标签的选项 我的代码在这里 window load function getJSON http localhost ABC web app dev php doctorFillOption function dat
  • Silverlight:组合框中的默认值

    我想在组合框中显示默认文本 例如 选择一个人 消息 你能帮我一下吗 请注意 我正在使用域上下文中的数据绑定 谢谢 为了实现这一点 我使用了派生的ExtendedComboBox扩展内置类ComboBox班级 您可以在以下位置找到该类的源代码