WPF 嵌套数据绑定;如何绑定到另一个项目内的项目

发布于 2024-11-08 20:34:17 字数 2481 浏览 0 评论 0原文

我有一个 ListView 绑定到 CustomerContacts 的 ObservableCollection。 到目前为止效果很好,但是对于这个新手,我不确定如何进行下一部分。 每个客户联系人都有多种联系人类型,我想将其显示在他们的姓名下。

因此,在 CustomerContacts 内部,我有另一个 ContactTypes 的 ObservableCollection。

这是我当前的数据模板:

    <DataTemplate x:Key="iconTemplate">
        <DockPanel Height="133" Width="150">
            <Image Source="/Tracks%203.5;component/Images/NoPic.png" Height="25" Width="25" Margin="1,0" />
            <TextBlock DockPanel.Dock="Top" Text="{Binding FullName}" Margin="5,3,5,0" FontWeight="Bold" HorizontalAlignment="Left" />
            <<TextBlock Text="{Binding Title}" Margin="5,0,5,3" HorizontalAlignment="Left" />>
        </DockPanel>
    </DataTemplate>

这是我第一次尝试将列表视图放入其中:

    <DataTemplate x:Key="iconTemplate">
        <DockPanel Height="133" Width="150">
            <Image Source="/Tracks%203.5;component/Images/NoPic.png" Height="25" Width="25" Margin="1,0" />
            <TextBlock DockPanel.Dock="Top" Text="{Binding FullName}" Margin="5,3,5,0" FontWeight="Bold" HorizontalAlignment="Left" />
            <ListView ItemsSource="{Binding ContactTypes}">
                <ListView.Template>
                    <ControlTemplate TargetType="ItemsControl">
                        <ItemsPresenter/>
                    </ControlTemplate>
                </ListView.Template>
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Margin="3,0,0,0" HorizontalAlignment="Center" Text="{Binding Path=ContactType}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <!--<TextBlock Text="{Binding Title}" Margin="5,0,5,3" HorizontalAlignment="Left" />-->
        </DockPanel>
    </DataTemplate>

我想用绑定到 ContactTypes 中的项目的 ListView/ListBox/ItemsControl 替换绑定到 Title 的 TextBlock。

有点类似于这个问题:< WPF:绑定到类内的列表 >但没有所有的代码隐藏。如果将其包含在 XAML 中就好了。

I have a ListView bound to an ObservableCollection of CustomerContacts.
It works great so far, but being new to this, I'm not sure how to do the next part.
Each customer contact has several contact types, which I want to display under their name.

So inside of CustomerContacts I have another ObservableCollection of ContactTypes.

Here is my current datatemplate:

    <DataTemplate x:Key="iconTemplate">
        <DockPanel Height="133" Width="150">
            <Image Source="/Tracks%203.5;component/Images/NoPic.png" Height="25" Width="25" Margin="1,0" />
            <TextBlock DockPanel.Dock="Top" Text="{Binding FullName}" Margin="5,3,5,0" FontWeight="Bold" HorizontalAlignment="Left" />
            <<TextBlock Text="{Binding Title}" Margin="5,0,5,3" HorizontalAlignment="Left" />>
        </DockPanel>
    </DataTemplate>

And here's my first attempt at putting the listview inside:

    <DataTemplate x:Key="iconTemplate">
        <DockPanel Height="133" Width="150">
            <Image Source="/Tracks%203.5;component/Images/NoPic.png" Height="25" Width="25" Margin="1,0" />
            <TextBlock DockPanel.Dock="Top" Text="{Binding FullName}" Margin="5,3,5,0" FontWeight="Bold" HorizontalAlignment="Left" />
            <ListView ItemsSource="{Binding ContactTypes}">
                <ListView.Template>
                    <ControlTemplate TargetType="ItemsControl">
                        <ItemsPresenter/>
                    </ControlTemplate>
                </ListView.Template>
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Margin="3,0,0,0" HorizontalAlignment="Center" Text="{Binding Path=ContactType}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <!--<TextBlock Text="{Binding Title}" Margin="5,0,5,3" HorizontalAlignment="Left" />-->
        </DockPanel>
    </DataTemplate>

I want to replace the TextBlock bound to Title with a ListView/ListBox/ItemsControl bound to the items in ContactTypes.

Somewhat similar to this question: < WPF: bind to a List inside a class > but without all the codebehind. It would be nice to have it in the XAML.

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

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

发布评论

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

评论(1

最终幸福 2024-11-15 20:34:17

我会在这里做几件事:

  1. 简化您尝试做的事情以隔离问题。而不是整个 ListView 业务,只需放入一个 ItemsControl将为您提供默认的事物视图,它可能只显示对象的数据类型(无论 ContactTypes 是什么),但它会让您知道绑定是否有效。如果是,您将在其中列出一些内容。如果不是,您就不会。

  2. 如果您没有列出任何内容,请使用 Snoop 深入研究并查找错误。 Snoop 显示数据绑定错误,允许您检查每个项目的 DataContext,等等。

  3. 如果您仍然遇到问题,如果您发布了类定义和连接代码,这可能会对我们有利。可能还存在一些其他潜在问题(例如,当绑定最初发生时,ContactTypes 属性是否为 null,并且您没有使用 INPC 让绑定系统知道它何时发生变化?)。

There's a couple things I would do here:

  1. Simplify what you're trying to do in order to isolate the problem. Instead of that whole ListView business, just put in there an ItemsControl: <ItemsControl ItemsSource="{Binding ContactTypes}" /> That'll give you the default view of things, which may just show you the data type of your objects (whatever ContactTypes is), but it'll let you know if the binding is working. If it is, you'll get something listed there. If it isn't, you won't.

  2. If you're not getting anything listed, go use Snoop to drill into it and look for errors. Snoop shows the databinding errors, allows you to inspect the DataContext of each of the items, etc.

  3. If you're still having problems, it might benefit us if you posted your class definitions and your code where you wire things up. There might be some other underlying issues (for instance, is the ContactTypes property null when the binding initially occurs and you're not using INPC to let the binding system know when it changes?).

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