如果 Silverlight 中第一个属性为 null,则绑定到第二个属性
我有一个类,其中对象的名称可以为空。
public class Thing
{
/// <summary>
/// The identifier of the thing
/// </summary>
/// <remarks>
/// This will never be null.
/// </remarks>
public string Identifier { get; set; }
/// <summary>
/// The name of the thing
/// </summary>
/// <remarks>
/// This MAY be null. When it isn't, it is more descriptive than Identifier.
/// </remarks>
public string Name { get; set; }
}
在 Silverlight ListBox 中,我使用 DataTemplate,其中将名称绑定到 TextBlock:
<DataTemplate x:Key="ThingTemplate">
<Grid>
<TextBlock TextWrapping="Wrap" Text="{Binding Name}" />
</Grid>
</DataTemplate>
但是,如果名称为 null,这显然看起来不太好。理想情况下,我想使用相当于
string textBlockContent = thing.Name != null ? thing.Name : thing.Identifier;
但我无法更改我的模型对象的东西。有什么好的方法可以做到这一点吗?
我考虑过使用转换器,但在我看来,我必须将转换器绑定到对象本身,而不是 Name 属性。这很好,但是当名称或标识符更改时我将如何重新绑定?如果我手动侦听对象的 INotifyPropertyChanged
事件,IValueConverter
似乎没有任何方法强制重新转换。
关于最好的方法有什么想法吗?
I have a class in which the name of the object can be null.
public class Thing
{
/// <summary>
/// The identifier of the thing
/// </summary>
/// <remarks>
/// This will never be null.
/// </remarks>
public string Identifier { get; set; }
/// <summary>
/// The name of the thing
/// </summary>
/// <remarks>
/// This MAY be null. When it isn't, it is more descriptive than Identifier.
/// </remarks>
public string Name { get; set; }
}
In a Silverlight ListBox, I use a DataTemplate where I have the name bound to a TextBlock:
<DataTemplate x:Key="ThingTemplate">
<Grid>
<TextBlock TextWrapping="Wrap" Text="{Binding Name}" />
</Grid>
</DataTemplate>
However, this obviously doesn't look very good if the Name is null. Ideally, I would want to use something equivalent to
string textBlockContent = thing.Name != null ? thing.Name : thing.Identifier;
but I can't change my model object. Is there any good way to do this?
I thought about using a Converter, but it seems to me I'd have to bind the converter to the object itself, and not the Name property. This would be fine, but how would I then rebind when either Name or Identifier changes? IValueConverter
doesn't appear to have any way to force a reconvert if I would manually listen on my object's INotifyPropertyChanged
event.
Any ideas on the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以更改绑定,以便它直接绑定到您的
Thing
实例:然后使用一个转换器,从传递的参数中返回
Instance
或Name
事物
实例You can change the Binding, so that it binds directly to your
Thing
instance:Then use a converter that returns either
Instance
orName
from the passedThing
instance在 WPF 中,您可以通过实现 IMultiValueConverter< 轻松完成此操作/a>.不幸的是,Silverlight 并不直接支持这一点,尽管有 为 Silverlight 编写的解决方法。
In WPF, you can do this easily by implementing an IMultiValueConverter. Unfortunately, Silverlight doesn't support this directly, though there are workarounds written for Silverlight.
Converter
未绑定到Name
或Object
,它绑定到Text
属性绑定。因此,每次为
Text
赋值时都会调用它。由您在Converter
内部选择Text
属性的外观。可能的解决方案之一是创建
NameAndIdentifier
字符串特殊属性,该属性可根据模型的Name
和Identifier
属性从 Converter 分配。The
Converter
is not bind toName
orObject
, it binds toText
property binding.So, it will be invoked every time you assign a value to
Text
. Up to you after inside aConverter
to choice whatText
property will look like.One of possible solutions is to make
NameAndIdentifier
string special property which is assignable from Converter based on Model'sName
andIdentifier
properties.您需要在 ViewModel/Presenter(或任何您想要的名称)中创建一个自定义显示属性。基本上,您上面发布的代码必须进行修改,并且添加了以下内容:
据我所知,任何其他方式都将是一种黑客行为。
You need to create a custom display property in your ViewModel/Presenter (or whatever you want to call it). Basically, the code you posted above must be modified, and this added to it:
Any other way, as far as I know, is going to be a hack.