我有一个类,其中对象的名称可以为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>
然而,如果名称为空,则显然看起来不是很好。理想情况下,我希望使用与以下等效的内容:
string textBlockContent = thing.Name != null ? thing.Name : thing.Identifier;
但我无法更改我的模型对象。有没有好的方法来解决这个问题?
我考虑使用转换器,但似乎我必须将转换器绑定到对象本身,而不是名称属性。这样也可以,但当名称或标识符更改时,我该如何重新绑定?IValueConverter
似乎没有任何方法可以强制重新转换,如果我手动监听我的对象的INotifyPropertyChanged
事件。
有关最佳方法的任何想法吗?