WPF绑定多个DataContext(代码隐藏) [英] WPF Binding multiple DataContexts (code-behind)

查看:363
本文介绍了WPF绑定多个DataContext(代码隐藏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我是WPF的初学者,请原谅.
我需要一个绑定到两个对象作为数据源的网格,并偶尔更新内容
我选了 http://www.switchonthecode.com/tutorials/wpf-tutorial-以the-listview-part-1 为例,效果很好

Hi!
I’m a beginner when it comes to WPF so please forgive me.
I need a grid which binds to two objects as datasources and occasionally updates the content
I took http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-1 as an example which works perfectly

<Window x:Class="WpfApplication17.MainWindow"

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

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

        DataContext="{Binding RelativeSource={RelativeSource Self}}"

        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <ListView Name="MyListview" ItemsSource="{Binding GameCollection}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Width="140" Header="Game Name" DisplayMemberBinding="{Binding GameName}"  />
                        <GridViewColumn Width="140" Header="Creator" DisplayMemberBinding="{Binding Creator}" />
                        <GridViewColumn Width="140" Header="Publisher" DisplayMemberBinding="{Binding Publisher}" />
                    </GridView>
                </ListView.View>
            </ListView>
            <Button HorizontalAlignment="Right" Margin="5,5,5,5" Content="Add Row" Click="AddRow_Click" />
        </StackPanel>
    </Grid>
</Window>



但是我在以下几行中遇到问题



However I have problems with the following lines

DataContext="{Binding RelativeSource={RelativeSource Self}}";


请保持友好,并向我解释这一行.
如何在后台没有代码的情况下重写没有RelativeSource绑定的代码,并将两个源添加到网格中



任何帮助表示赞赏

提前谢谢您!


Mat


Please be so nice and explain this line to me.
How do I rewrite the code without the RelativeSource binding in code-behind and add two sources to the grid



Any help is appreciated

Thanks you in advance!


Mat

推荐答案

我发现了一些可以帮助我的东西:

http://social.msdn.microsoft.com/Forums/zh/wpf/thread/79d10780-12b6-4aac-93b8-c18eef949b07
I found something to help me out a little :

http://social.msdn.microsoft.com/Forums/en/wpf/thread/79d10780-12b6-4aac-93b8-c18eef949b07


这篇关于WPF绑定多个DataContext(代码隐藏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆