我有一个自定义的 ListBox 项, 我试图在程序上添加到 ListBox 中, 我希望该项目的内容可以包装 。
以下是自定义的列表BoxItem :
class PresetListBoxItem : ListBoxItem
{
public uint[] preset;
public PresetListBoxItem(uint[] preset = null, string content = "N/A")
: base()
{
this.preset = preset;
this.Content = content;
}
}
和XAML:
<ListBox Name="sortingBox" Margin="5,5,0,5" Width="150" MaxWidth="150" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="2" CornerRadius="3" Margin="3">
<TextBlock Text="{Binding Path=Text}" TextWrapping="WrapWithOverflow" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
和代码 做添加:
PresetListBoxItem item = new PresetListBoxItem();
item.preset = new uint[] { };
item.Content = "This is a test of an extended line of text.";
sortingBox.Items.Add(item);
当我运行代码时,项目会被添加到盒子里, 但边框完全没有出现, 它也不会包装线条。
我查遍了SO和Google的答案, 我同时使用ListBoxs和ListBoxs 和ListPhision, 但似乎没什么用。