English 中文(简体)
自定义列表框添加程序时的 Word word wrap 字
原标题:Wordwrap for Custom ListBoxes Added Programmatically

我有一个自定义的 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, 但似乎没什么用。

最佳回答

ListBoxTroject 仅仅是内容的容器。 如果您想要使用您自己的 ListBoxTroject 来取代容器的模板, 而不是项目。 那么对于在 TextBlock 中绑定的权利, 您必须绑定到您 PresetLoxBoxText的 Content 属性 。

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:PresetListBoxItem">
                    <Border BorderBrush="Black" BorderThickness="2" CornerRadius="3" Margin="3">
                        <TextBlock Text="{Binding Path=Content, RelativeSource={RelativeSource AncestorType=local:PresetListBoxItem}}"
                                   TextWrapping="WrapWithOverflow" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ListBox.ItemContainerStyle>

但我认为这不是最好的方式。 为什么你要从 < code> ListBoxTroject 中获取? 如果你不这样做, 您的 XAML 马上就可以了 。

item.Text = "This is a test of an extended line of text.";

class PresetListBoxItem
{
    public uint[] preset;
    public string Text { get; set; }

    public PresetListBoxItem(uint[] preset = null, string content = "N/A")
      : base()
    {
        this.preset = preset;
        this.Text = content;
    }
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签