简体   繁体   English

如何在WPF列表框中呈现默认文本

[英]How to render a default text in a wpf listbox

I am quite new to wpf, so I have been having troubles with some listbox bindings, here's my scenario: 我对wpf还是很陌生,所以我遇到了一些列表框绑定的麻烦,这是我的情况:

I have an ObservableCollection bound to my listbox. 我有一个ObservableCollection绑定到我的列表框。 Let's call this collection "Templates" 我们将此集合称为“模板”

This is my MessageTemplateClass class: 这是我的MessageTemplateClass类:

public class MessageTemplate{
        public virtual long Id { get; set; }
        public virtual String Name { get; set; }
        public virtual MessageTemplateType MessageTempleateType { get; set; } //some enum
        public virtual String Topic{ get; set; }
        public virtual String Body{ get; set; }

        public override string ToString()
        {
            return Name;
        }
}

I have my Listbox bound to this Templates property: 我将列表框绑定到此Templates属性:

<ListBox Name="lstTemplates" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"   
                 ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                 ItemsSource="{Binding Templates}" 
                 SelectedItem="{Binding Path=CurrentTemplate}"
                 DisplayMemberPath="Name">

CurrentTemplate is the currently selected template (which users can edit). CurrentTemplate是当前选择的模板(用户可以编辑)。 There is a button which, when clicked, does this: 有一个按钮,单击该按钮即可:

CurrentTemplate = new MessageTemplate();
Templates.Add( CurrentTemplate);

Now, I would like the listbox to display these Templates (which have no name assigned) with some default text, such as "Unnamed *" or something. 现在,我希望列表框显示这些模板(未分配名称)以及一些默认文本,例如“未命名*”或其他内容。

So here are my two questions:" 所以这是我的两个问题:”

1) How do I specify that if the lisbox item's (message template) name is not set, display an "Unnamed *" label / textblock for it? 1)如何指定如果未设置lisbox项的(消息模板)名称,则为其显示“未命名*”标签/文本块?

2) This is kind of off-topic but when I edit a new template, When I assign a name to it, it will dissapear from the listbox (the empty space in the listbox goes away). 2)这有点离题,但是当我编辑一个新模板时,当我给它分配一个名称时,它将从列表框中消失(列表框中的空白区域消失了)。 Any idea on why could this be? 知道为什么会这样吗?

Any help is greatly appreciated 任何帮助是极大的赞赏

EDIT: 编辑:

I am looking to do this strictly via xaml. 我希望通过xaml严格执行此操作。 Assume I have no control over my MessageTemplate class 假设我无法控制我的MessageTemplate类

To set a default String if name is empty you can declare your property like this 要设置默认字符串(如果名称为空),可以这样声明属性

private string _name = "";
public string Name { 
get
{
    if(string.IsNullOrEmpty(_name))
    {
        return "Unnamed";
    }
    else
    {
        return _name;
    }
}
 set
{
    _name = value;
}
}

Try FallBackValue and TargetNullValue on the binding. 在绑定上尝试FallBackValue和TargetNullValue。 You can also go with IValueConverter. 您也可以使用IValueConverter。

Update: Here you can find an example of how to use TargetNullValue: http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.targetnullvalue(v=vs.110).aspx 更新:在这里您可以找到有关如何使用TargetNullValue的示例: http : //msdn.microsoft.com/zh-cn/library/system.windows.data.bindingbase.targetnullvalue( v=vs.110) .aspx

If the value on your binding is null you can display a default text. 如果绑定上的值为null,则可以显示默认文本。 It will look like this: 它看起来像这样:

<TextBox Text="{Binding SomeProperty, TargetNullValue=your default value}" />

FallBackValue will work in a similar fashion: http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.fallbackvalue(v=vs.110).aspx FallBackValue将以类似的方式工作: http : //msdn.microsoft.com/zh-cn/library/system.windows.data.bindingbase.fallbackvalue( v=vs.110) .aspx

The last option I mentioned is the IValueConverter: http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx 我提到的最后一个选项是IValueConverter: http : //msdn.microsoft.com/zh-cn/library/system.windows.data.ivalueconverter.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM