简体   繁体   English

如何使用 ListBox 作为基础 class?

[英]How can I use ListBox as base class?

I use ListBox to show some grid.我使用ListBox来显示一些网格。 I want to create some class that will be based on (derived from) the ListBox and to change the ItemTemplate of this derived class.我想创建一些基于(派生自) ListBox的 class,并更改此派生 class 的ItemTemplate

How can i do it?我该怎么做?

I try to define a class that derives from UserControl class - and change the derived class to be ListBox beside the UserControl class - but this doesn't work. I try to define a class that derives from UserControl class - and change the derived class to be ListBox beside the UserControl class - but this doesn't work.

Is there any other way?还有其他方法吗?

I need to have the ability to change the ListBoxItem template in the ListBox like I do in the ListBox that I add to simple silverlight page.我需要能够更改ListBox中的ListBoxItem模板,就像我在添加到简单 silverlight 页面的ListBox中所做的那样。

Thanks for any help.谢谢你的帮助。

what you can do is to create a templated control (you can easily create one via visual studios Add - New Item menu) whcich you derive from ListBox:您可以做的是创建一个模板化控件(您可以通过 Visual Studios Add - New Item 菜单轻松创建一个),您从 ListBox 派生:

public class MyListBox : ListBox
{
    public MyListBox()
    {
        this.DefaultStyleKey = typeof(MyListBox);
    }
}

When you create a templated control it will also create the folder Themes and in there a generic.xaml where your styles are defined.当您创建模板化控件时,它还将创建文件夹 Themes 并在其中创建 generic.xaml ,其中定义了您的 styles。 After that take the ListBox - Style from here and copy and paste it in your generic.xaml at the right position (you need the ValidationTooltipTemplate, too):之后,从此处获取 ListBox - Style 并将其复制并粘贴到您的 generic.xaml 右侧 position 中(您也需要 ValidationTooltipTemplate):

<Style TargetType="local:MyListBox">
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Background" Value="#FFFFFFFF" />
        <Setter Property="Foreground" Value="#FF000000"/>
        <Setter Property="HorizontalContentAlignment" Value="Left" />
        <Setter Property="VerticalContentAlignment" Value="Top" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="TabNavigation" Value="Once" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="BorderBrush"> ...

Ok, after that define you ItemTemplate in the same xaml:好的,之后在同一个 xaml 中定义您的 ItemTemplate:

<DataTemplate x:Key="MyItemTemplate">

and add it to the Setter List of your ListBox Style:并将其添加到您的 ListBox 样式的 Setter 列表中:

<Style TargetType="local:MyListBox">
        <Setter Property="ItemTemplate" Value="{StaticResource MyItemTemplate}"/>^

Now it is your default template.现在它是您的默认模板。

But do you need to add any specific behaviour to your ListBox, because you want to inherit from it.但是您是否需要向您的 ListBox 添加任何特定行为,因为您想从它继承。 If you just want to add the specific item template, i would not recommend to derive from ListBox (in fact, the DataTemplate is not generic, it is always specific for a DataType).如果您只想添加特定的项目模板,我不建议从 ListBox 派生(实际上,DataTemplate 不是通用的,它始终特定于 DataType)。 If you don't add specific behaviour I would go for creating a global DataTemplate (maybe defined in your app.xaml) and reference it as DataTemplate everytime you need it or I would create a specific style with this DataTemplate and set the style to the common ListBox.如果您不添加特定行为,我会 go 用于创建全局 DataTemplate(可能在您的 app.xaml 中定义)并在每次需要时将其作为 DataTemplate 引用,否则我将使用此 DataTemplate 创建特定样式并将样式设置为常见的列表框。

If you have any questions, just leave a comment.如果您有任何问题,请发表评论。

Hope this helps!希望这可以帮助!

BR, BR,

TJ TJ

Other way to doit, if you want that your control looks by default as a ListBox.其他方法,如果您希望您的控件默认显示为 ListBox。 Just create a class derived from ListBox, and on the default constructor modify the DefaultStyleKey:只需创建一个从 ListBox 派生的 class,然后在默认构造函数上修改 DefaultStyleKey:

public class MyListBox : ListBox
{
    public MyListBox()
    {
        // the control would look like a listBox by default
        DefaultStyleKey = typeof (ListBox);
    }

    (...)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM