如何将标签放入WPF ListBox?

时间:2014-12-15 13:19:44

标签: wpf

我如何设置标签?与

   <ListBox x:Name="ListBoxContents" /> 

在xaml中?该ListBox中位置20和50处的选项卡。 ListBox文本是Name \ tAddress \ tCity。

2 个答案:

答案 0 :(得分:0)

您可以使用字符&#x09;和属性xml:space="preserve",如下所述:

using tab and carriage return character in a WPF resource dictionary

要用\t替换&#x09;,您可以在绑定中使用ValueConverter:

public class TabConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if(value is string)
        {
            return value.ToString().Replace("\t", "&#x09;");
        }
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if(value is string)
        {
            return value.ToString().Replace("&#x09;", "\t");
        }
        return value;
    }
}

列表框中的绑定可能如下:

<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding NameAndAddress Converter=TabConverter}" />
    </DataTemplate>
</ListBox.ItemTemplate>

答案 1 :(得分:0)

我在以下网址找到了相关信息:Multicolumn ListBox in WPF - &gt;第二个答案描述了使用ListBox。下面是我添加的代码......它有效。

public MainWindow()
{
  InitializeComponent();
  List<ListBoxItems> items = new List<ListBoxItems>();
  items.Add(new ListBoxItems() { ColumnOneText = "First LIne", ColumnTwoText = "  45", ColumnThreeText = "  End" });
  items.Add(new ListBoxItems() { ColumnOneText = "Line 2", ColumnTwoText = "  45 ,,, 2", ColumnThreeText = "  End -> 2" });
  items.Add(new ListBoxItems() { ColumnOneText = "Line 3", ColumnTwoText = "  45 ,,,,,, 3", ColumnThreeText = "  End -> 3" });
  gLBxDb2.ItemsSource = items;
  }
}
public class ListBoxItems
{
    public string ColumnOneText { get; set; }
    public string ColumnTwoText { get; set; }
    public string ColumnThreeText { get; set; }
}