将 Listview 绑定到多个源

发布于 2024-12-12 08:20:49 字数 2149 浏览 0 评论 0 原文

我面临问题,如果您能提供帮助,我会很高兴。 因此,我将“Listview”绑定到“Feeds”对象,该对象是一个 FeedViewModel 对象。

但在某些时候,我想显示一些数据,这些数据不存在于 FeedViewModel 对象中,但存在于 MainViewModel 对象中。 就我而言,例如,我想显示每个提要的 URL,但 URL 不是从源的 Xpath 提取中提取的,而是从 MainViewModel 对象传递的,我已通过 MainViewModel.Url 在代码中显示了该对象。

但在我的 XAML 中,Listview 的所有子级都只查看“Feeds”对象,这会产生问题。

 <ListView Grid.Row="3" Margin="5" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Feeds}">
        <ListView.ItemTemplate>
            <DataTemplate>
                        --------------some code --------------
                        <Label Margin="4">Critic:</Label>
                        <Label Grid.Column="1" Content="{Binding Creator}" Margin="4" />
                        <Label Grid.Row="1" Margin="4">Title:</Label>
                        <Label Grid.Column="1" Grid.Row="1" Content="{Binding Title}" Margin="4" FontWeight="Bold" />
                        <Label Grid.Row="2" Margin="4">Location:</Label>
                        <Label Grid.Column="1" Grid.Row="2" Content="{Binding **MainViewModel.Url**}" Margin="4" />
                        <Label Grid.Row="3" Margin="4">Date:</Label>
                        <Label Grid.Column="1" Grid.Row="3" Content="{Binding Date}" Margin="4" />
                        <Label Grid.Row="4" Margin="4">Rating:</Label>
                        <Label Grid.Column="1" Grid.Row="4" Content="{Binding Rating}" Margin="4" />
                        <Label Grid.Row="5" Margin="4" HorizontalAlignment="Stretch">Description:</Label>
                        <TextBlock Grid.Column="1" Grid.Row="5" TextWrapping="Wrap" ScrollViewer.VerticalScrollBarVisibility="Auto" Text="{Binding Description}" Padding="4" Margin="4" />
                    </Grid>
                </Border>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

有人可以建议可以做什么吗?

高拉夫

I am facing problem and would be glad if you could help .
So,I am binding "Listview" to "Feeds" object which is a FeedViewModel object.

But at some point ,I want to show some data which isn't present in the FeedViewModel object but is present in the MainViewModel object.
In my case,for ex-i want to display the URL for every feed but the URL isn't being extracted from the Xpath extraction of source but being passed from the MainViewModel object which I have shown in code by MainViewModel.Url.

But in my XAML,all the children of the Listview look only in the "Feeds" object which is creating a problem.

 <ListView Grid.Row="3" Margin="5" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Feeds}">
        <ListView.ItemTemplate>
            <DataTemplate>
                        --------------some code --------------
                        <Label Margin="4">Critic:</Label>
                        <Label Grid.Column="1" Content="{Binding Creator}" Margin="4" />
                        <Label Grid.Row="1" Margin="4">Title:</Label>
                        <Label Grid.Column="1" Grid.Row="1" Content="{Binding Title}" Margin="4" FontWeight="Bold" />
                        <Label Grid.Row="2" Margin="4">Location:</Label>
                        <Label Grid.Column="1" Grid.Row="2" Content="{Binding **MainViewModel.Url**}" Margin="4" />
                        <Label Grid.Row="3" Margin="4">Date:</Label>
                        <Label Grid.Column="1" Grid.Row="3" Content="{Binding Date}" Margin="4" />
                        <Label Grid.Row="4" Margin="4">Rating:</Label>
                        <Label Grid.Column="1" Grid.Row="4" Content="{Binding Rating}" Margin="4" />
                        <Label Grid.Row="5" Margin="4" HorizontalAlignment="Stretch">Description:</Label>
                        <TextBlock Grid.Column="1" Grid.Row="5" TextWrapping="Wrap" ScrollViewer.VerticalScrollBarVisibility="Auto" Text="{Binding Description}" Padding="4" Margin="4" />
                    </Grid>
                </Border>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Can someone suggest what can be done?

Gaurav

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

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

发布评论

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

评论(1

薯片软お妹 2024-12-19 08:20:49

DataContext更改为模板化项目,要访问主视图模型,您可以使用 DataContext href="http://msdn.microsoft.com/en-us/library/system.windows.data.binding.relativesource.aspx" rel="nofollow">RelativeSource,例如

<Label Grid.Column="1" Grid.Row="2"
       DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ListView}}"
       Content="{Binding Url}" Margin="4" />

The DataContext is changed to the templated item, to get to the main viewmodel you can target a parent's DataContext using RelativeSource, e.g.

<Label Grid.Column="1" Grid.Row="2"
       DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ListView}}"
       Content="{Binding Url}" Margin="4" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文