简体   繁体   English

如何将 wpf ListBox 的 ItemTemplate 嵌入到 Window 的资源中?

[英]How do I embed the ItemTemplate for a wpf ListBox into the Window's resources?

Sorry if this is a basic question, but how can I take an ItemTemplate that I have for a ListBox, and put it in the resources for the window so that more than one ListBox can use it.抱歉,如果这是一个基本问题,但我如何才能将我拥有的 ListBox 的 ItemTemplate 放入窗口的资源中,以便多个 ListBox 可以使用它。

Here's some XAML:这是一些 XAML:

<Window x:Class="Example">
    <Window.Resources>
        <DataTemplate x:Key="dtExample">
            <ListBox.ItemTemplate>
            // styles go here...
            </ListBox.ItemTemplate>
        </DataTemplate>
    </Window.Resources>
    <ListBox ItemTemplate="{StaticResource dtExample}">
    // items go here...
    </ListBox>
</Window>

This is throwing a "Attached property has no setter" design-time error.这会引发“附加属性没有设置器”设计时错误。 I've removed portions of code that I didn't think would matter, for sake of brevity.为简洁起见,我删除了我认为无关紧要的部分代码。

Thanks谢谢

just add your itemtemplate to your window's resource and add a key:只需将您的 itemtemplate 添加到您的窗口资源并添加一个键:

<Window.Resource>
 <DataTemplate x:Key="myTemplate">
  ....
 </DataTemplate>
</Window.Resources>

and then apply it with something like this:然后用这样的东西应用它:

<ListBox ItemTemplate="{StaticResource myTemplate}">
 ...
</ListBox>

you provided the following code:您提供了以下代码:

 <DataTemplate x:Key="dtExample">
        <ListBox.ItemTemplate>
        // styles go here...
        </ListBox.ItemTemplate>
    </DataTemplate>

but this will not work.但这行不通。 you cannot provide <ListBox.ItemTemplate> directly within your template.不能直接在模板中提供<ListBox.ItemTemplate> you don't need this here.你在这里不需要这个。 just create a simple datatemplate and it should work.只需创建一个简单的数据模板,它应该可以工作。

I know that the post is too old to be interesting for the author, yet i may be interesting for those who have the same problem and google it.我知道这篇文章太旧了,作者不感兴趣,但我可能对那些有同样问题的人感兴趣,然后用谷歌搜索。 As I may see the problem is you should use ListBox.ItemTemplate inside ListBox.正如我所看到的,问题是您应该在 ListBox 中使用 ListBox.ItemTemplate。 For instance, <ListBox ...><ListBox.ItemTemplate> ... </ListBox.ItemTemplate></ListBox>例如, <ListBox ...><ListBox.ItemTemplate> ... </ListBox.ItemTemplate></ListBox>

the subject is old but here is the solution:这个主题很旧,但这是解决方案:

<Window.Resources>
    <Style x:Key="ListBoxItem_Color" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            *//style*
        </Setter>
    </Style>
</Window.resources>

<ListBox x:Name="MyListBox"
         ...
         ItemContainerStyle="{StaticResource ListBoxItem_Color}">
      <.../>
</ListBox>

I think the problem is that you should x:Key properties in your resources instead of the x:Name..我认为问题在于您应该在资源中使用 x:Key 属性而不是 x:Name ..

Change that, and it will work like a charm :)改变它,它会像魅力一样工作:)

Do you have the following tags in your Window class?您的 Window 类中有以下标签吗?

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

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

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