C#WPF两种不同的DataContext控制于一体,同时使用MVVM和谢胜利,属性是每次都一样 [英] C# WPF two different DataContext in One Control while using MVVM and the secound property is every time the same

查看:299
本文介绍了C#WPF两种不同的DataContext控制于一体,同时使用MVVM和谢胜利,属性是每次都一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,code:

<ListView ItemsSource="{Binding DataTransferModel.Output}" Background="Transparent" Margin="0" VerticalContentAlignment="Top" AlternationCount="2" Name="lvOutput" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="2">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="0,1">
                <UserControls:OutputTextBox Text="{Binding Data, Mode=OneWay}" 
                                            IsReadOnly="True" 
                                            Grid.Row="2" 
                                            TextWrapping="WrapWithOverflow" 
                                            SelectedValue="{Binding Path=DataContext.SelectedOutput, 
                                                            Mode=TwoWay, 
                                                            UpdateSourceTrigger=PropertyChanged, 
                                                            RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}
                                                            }" 
                                            />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

和问题是,物业数据在OutputTextBox控制来自列表,但属性 SelectedOutput 应来自主要的DataContext视图模型。而属性 SelectedOutput 应该是列表中的每个条目是相同的。但是目前我们工作不到风度。 (

And the problem is that the property Data on the OutputTextBox control comes from the list, but the property SelectedOutput should come from the main DataContext the ViewModel. And the property SelectedOutput should be the same for every entry in the list. But currently it dosn't work. :(

推荐答案

贵的ListView被封闭在标签,如果是做网格具有从视图模型DataContext的?如果是它应该工作。

does your listview is enclosed in tags and if yes does the grid has the DataContext from the ViewModel? if yes it should work.

你看到在输出窗口中的任何绑定错误?也许你应该尝试使用史努比,了解你的真实的SelectedValue的结合。

do you see any binding errors in your output window? maybe you should try using Snoop to see your real binding of SelectedValue.

编辑:请尝试其他类型一格,然后,也许ListView中如果有视图模型的DataContext

Please try a Type other then Grid, maybe ListView if it has the ViewModel DataContext.

<UserControls:OutputTextBox Text="{Binding Data, Mode=OneWay}" 
                                        IsReadOnly="True" 
                                        Grid.Row="2" 
                                        TextWrapping="WrapWithOverflow" 
                                        SelectedValue="{Binding Path=DataContext.SelectedOutput, 
                                                        Mode=TwoWay, 
                                                        UpdateSourceTrigger=PropertyChanged, 
                                                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}
                                                        }" 
                                        />

这篇关于C#WPF两种不同的DataContext控制于一体,同时使用MVVM和谢胜利,属性是每次都一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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