xaml - TextBlock 绑定(bind)显示类名而不是空字符串

标签 xaml binding windows-runtime microsoft-metro winrt-xaml

我有以下 Windows RT 应用程序。我将字符串列表绑定(bind)到 TextBlock 的 ItemsControl。这会将空字符串显示为“System.Collections.Generic.List'1[System.String]”,而不仅仅是一个空字符串。我希望它显示一个空字符串而不是 DataContext 的类型。

后面的代码:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        DataContext = new List<string>() { "", "not empty string" };
    }
}

xml:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" FontSize="25"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

输出:
System.Collections.Generic.List'1[System.String]
non empty string

我用传统的 wpf 做了同样的例子,它正确显示了空字符串。

编辑
这输出相同的东西。

后面的代码:
public class Model
{
    private readonly List<string> items = new List<string>() { "", "non empty string" };

    public List<string> Items
    {
        get { return items; }
    } 
}

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        DataContext = new Model();
    }
}

xml:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <ItemsControl ItemsSource="{Binding Path=Items}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" FontSize="25"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

最佳答案

通过将转换器添加到 TextBlock,您实际上可以看到这是一个错误(或奇怪的故意功能)。 Binding .

添加静态资源:

<Page.Resources>
    <local:NoNullsConverter x:Key="fixNulls"></local:NoNullsConverter>
</Page.Resources>

然后更改 Binding 以引用 Converter:
<TextBlock Text="{Binding Converter={StaticResource fixNulls}}" FontSize="25"/>

添加这个类:
public class NoNullsConverter : IValueConverter
{
    // This converts the value object to the string to display.
    public object Convert(object value, Type targetType,
        object parameter, string language)
    {
        return value is string ? value : "";         
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

如果您在 return 上设置断点语句,您会看到实际传递的第一个值是整个列表。是的,出乎意料。但是,如果您按原样使用此转换器,它会处理这种奇怪的情况并返回一个更合乎逻辑的空字符串。

或者,您可以做一些更有趣的事情并创建一个简单的包装类:
public class StringContext
{
    public string Value { get; set; }
    public static implicit operator StringContext(string value)
    {
        return new StringContext() { Value = value };
    }

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

使用此类,您可以按预期使用 Binding :
<TextBlock Text="{Binding}" FontSize="25"/>

但是,您需要在 List 的声明中使用不同的类类型:
DataContext = new List<StringContext>() { "", "not empty string" };

使用隐式转换,它在转换 String 时“正常工作”到 StringContext .是的,它会增加创建不必要的类的开销,但它确实有效。 :) 我更喜欢 Converter 选项。

关于xaml - TextBlock 绑定(bind)显示类名而不是空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15930967/

相关文章:

c# - 如何在 Windows Phone 8 中覆盖不同分辨率的样式?

Silverlight 本地化

javascript - AngularJS 指令 - 来自 $scope 的模板和其他指令

c# - Windows 电话 8 : Media file access

visual-studio - 使 Visual Studio 2013 使用 XAML 自关闭标签

WPF - 在样式中使用 ControlTemplate 资源

c# - PropertyChangedEventHandler 为空

c# - 与 ElementName 的绑定(bind)不适用于 Windows XP 上的 DataTemplate

c# - 如何在 ComboBox 的开头和结尾显示 ComboBoxItem

timer - 如何在 Metro Style App 中实现计时器