我可以将数据绑定到自定义类(在 xaml 中实例化)的属性,然后形成模板化列表框项的内容吗

发布于 2024-10-07 00:12:07 字数 1666 浏览 1 评论 0原文

对此的任何帮助都非常感激。总之,我尝试将数据绑定到在 xaml 中实例化的自定义类的属性,然后形成模板化列表框项的内容(唷!)。

我有一个简单的 C# 类,名为 MenuItem。它有两个属性: - 标题 - 图标

仅专注于这些菜单项之一(即提供一个简单的示例来说明我陷入困境的位置)如果我这样做(使用硬编码的值),它可以正常工作:

<ListBox>
   <ListBoxItem ContentTemplate="{StaticResource MenuItemTemplate}">
        <myclasses:MenuItem Heading="News" IconImage="News.png"/>
    </ListBoxItem>
</Listbox>

其中 MenuItemTemplate 是绑定每个菜单项的资源部分中的适当 DataTemplate属性)包含以下行:

<TextBlock x:Name="tbHeading" Text="{Binding Heading}">

当我尝试使用绑定来设置标题属性时,它会失败(AG_E_PARSER_BAD_PROPERTY_VALUE 错误) - 例如:

<ListBox>
   <ListBoxItem ContentTemplate="{StaticResource MenuItemTemplate}">
        <myclasses:MenuItem Heading="{Binding NewsHeading, Mode=OneWay}" Icon="News.png"/>
    </ListBoxItem>
<Listbox>

我想知道这是否是因为我正在执行某种双重绑定(即模板绑定到需要绑定的 MenuItem 类上的值),这是不可能的吗?我尝试过将属性声明为依赖属性,但没有区别(尽管我今天才了解这些属性,所以我可能会遗漏一些东西)。

我知道我可以在视图模型中设置 menuitem 对象,并从那里绑定,但我想了解为什么上面的方法不起作用(就我的目的而言,在 xaml 中构造菜单项有优势)。

谢谢你!!!! 伊恩


感谢您坚持这一点。我同意可能不需要列表框 - 但即使我将其减少为内容控件中的一项:

<ContentControl ContentTemplate="{StaticResource MenuItemTemplate}">
    <myclasses:MenuItem Heading="{Binding NewsHeading, Mode=OneWay}" IconImage="News.png"/>
</ContentControl>

我仍然遇到同样的问题 - 即我可以让数据绑定在内容控件的内容中工作(在它被删除之前)由 ContentTemplate 中引用的数据模板呈现)使用纯 xaml。

即上面的 xaml 不起作用 - 它在绑定 NewsHeading 的位上抛出错误:

Heading="{Binding NewsHeading, Mode=OneWay}

所以我试图了解我正在做的事情是否不可能,或者是否不可能,但我做错了。

谢谢。

Any help on this really appreciated. In summary I'm trying to databind to properties of a custom class instantiated in xaml that then forms the content of a templated listboxitem (phew!).

I have a simple c# class called MenuItem. It has two properties:
- Heading
- Icon

Concentrating on just one of those menu items (i.e. to provide a simple example of where I am stuck) If I do this (with the values hard coded) it works fine:

<ListBox>
   <ListBoxItem ContentTemplate="{StaticResource MenuItemTemplate}">
        <myclasses:MenuItem Heading="News" IconImage="News.png"/>
    </ListBoxItem>
</Listbox>

Where MenuItemTemplate is an appropriate DataTemplate in the resources section binding each property) containing lines such as:

<TextBlock x:Name="tbHeading" Text="{Binding Heading}">

Wheareas when I try to use binding to set the Heading property it falls over (AG_E_PARSER_BAD_PROPERTY_VALUE error)- e.g.:

<ListBox>
   <ListBoxItem ContentTemplate="{StaticResource MenuItemTemplate}">
        <myclasses:MenuItem Heading="{Binding NewsHeading, Mode=OneWay}" Icon="News.png"/>
    </ListBoxItem>
<Listbox>

I've wondered if it is because I'm doing some kind of double binding (i.e. the template is binding to a value on the MenuItem class that needs to be bound) and that's not possible? I've tried having the properties declared as dependency properties but no difference (although I only learned about those today so I may be missing something).

I know I could set the menuitem objects up in the view model, and bind from there, but I would like to understand why the above doesn't work (as for my purposes there are advantages in constructing the menu items in the xaml).

Thank you!!!!
Ian


thanks for sticking with this. I agree the listbox might not be needed - but even if I reduce it to just one item in a contentcontrol:

<ContentControl ContentTemplate="{StaticResource MenuItemTemplate}">
    <myclasses:MenuItem Heading="{Binding NewsHeading, Mode=OneWay}" IconImage="News.png"/>
</ContentControl>

I still have the same problem - which is that I can get databinding to work within the content of a contentcontrol (prior to it being presented by the datatemplate referred to in ContentTemplate) using purely xaml.

I.e. the above bit of xaml doesn't work - it throws an error on the bit that binds the NewsHeading:

Heading="{Binding NewsHeading, Mode=OneWay}

So I am trying to understand whether what I'm doing is impossible, or whether it is but I'm doing it wrong.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴意少年 2024-10-14 00:12:07

假设您有多个 MenuItem 类(因为您将它们放入列表框中,如果您只有一个,那么这样做就没有意义)。您需要将集合绑定到ListBoxItemsSource 属性。

像这样的事情:

<ListBox ItemsSource="{Binding MyMenuItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding Heading}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

请注意,上面假设您已将页面上的 DataContext 设置为一个具有名为 MyMenuItems 属性的对象,该属性是 MenuItem 对象的集合。

要查看完整的示例,请查看创建新的“Windows Phone 数据绑定应用程序”时创建的默认代码。

编辑:
根据您的评论,ListBox 似乎不是最适合您需求的解决方案。列表框被设计/旨在获取项目集合并将它们显示在列表中。

如果您有许多在设计时了解的不同对象,并且只是希望将它们一个放在另一个之上(给出列表的外观),您可以简单地将它们放入 ScrollViewer 和/或 StackPanel(或其他合适的容器)。另外,如果您这样做,您仍然可以进行数据绑定。

Assuming that you have multiple MenuItem classes (because you're putting them in a listbox and ti wouldn't make sense to do that if you just had one). You need to bind the collection to the ItemsSource property of the ListBox.

Somehting like this:

<ListBox ItemsSource="{Binding MyMenuItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding Heading}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Note that the above assumes you've set the DataContext on the page to an object with a property called MyMenuItems which is a collection of your MenuItem objects.

To see a full example of this, look at the default code created when you create a new "Windows Phone Databound Application".

Edit:
Based on your comments, it seems that a ListBox is not the most appropriate solution to your needs. A ListBox is designed/intended to take a collection of items and display them in a list.

If you have a number of different objects which you know about at design time and simply wish to have them one on top of another (giving the appearance of a list) you could simply put them inside a ScrollViewer and/or a StackPanel (or other appropriate container). Plus, you would still be able to databind if you did it this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文