WPF中DataContext的继承 [英] Inheritance of DataContext in WPF
问题描述
我在窗口中有一个用户控件。我需要一个用于Window的DataContext和另一个用于UserControl的DataContext。问题是Window中的DataContext从User Control继承到DataContext。这意味着,在初始化应用程序后,我从UserControl中丢失了我的DataContext,但是同时从Window和UserControl获取了Window的DataContext。
当我没有设置Window的DataContext时,我的用户控件就是正确的。这意味着,每个DataContext分配都适用于它自己,但不能在一起工作。
窗口的我的Xaml:
< 窗口 x:Class = myproj.MainWindow
xmlns = http ://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns: x = http://schemas.microsoft.com/winfx/2006/xaml
< span class =code-attribute> xmlns:sys = clr- namespace:System; assembly = mscorlib
xmlns:my = clr-namespace:myproj.Controls
< span class =code-attribute> xmlns:myBehaviour < span class =code-keyword> = clr-namespace:行为
< span class =code-attribute> 标题 = window WindowState = 最大化 myBehaviour:Behaviour.Command = {Binding Command} >
< 网格 >
< Grid.ColumnDefinitions >
< ColumnDefinition < span class =code-attribute> x:名称 = TreeViewColumn 宽度 = 250 / >
< ColumnDefinition x:Na我 = MainColumn MinWidth = 200 / >
< / Grid.ColumnDefinition >
< Grid.RowDefinitions >
< RowDefinition < span class =code-attribute> x:名称 = MenuBarRow 高度 = 56 / >
< RowDefinition x:名称 = MainRow MinHeight = 200 高度 = 200 * / >
< / Grid.RowDefinition >
< my:UserControl Grid.Column = 0 Grid.Row = 1 HorizontalAlignment = 拉伸 VerticalAlignment = 拉伸 >
< / Grid >
< / Window >
我的代码 - 窗口的构造函数
public MainWindow()
{
this .DataContext = new MainViewModel();
InitializeComponent();
}
我的用户控件Xaml
< pre lang =xml> < UserControl x:Class = myproj.Controls.myUserControl
xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml
xmlns:mc = http:// schemas .openxmlformats.org / markup-compatibility / 2006
< span class =code-attribute> xmlns:d = http:/ /schemas.microsoft.com/expression/blend/2008\"
mc:Ignorable = d
< span class =code-attribute> d:DesignHeight = 300 d:DesignWidth = 300 > ;
< 做ckPanel >
< TextBox TextWrapping < span class =code-keyword> = 换行 HorizontalAlignment = 拉伸 VerticalAlignment = 拉伸 文字 = {Binding myProperty,UpdateSourceTrigger = PropertyChanged} DockPanel.Dock = Top / >
< / DockPanel >
< / UserControl >
我的代码 - 用户控件的构造函数后面
public UserControl()
{
InitializeComponent();
this .DataContext = new UserControlViewModel;
}
我希望有人能给我一些建议。
谢谢。
是的,它已经解决了,
现在我的主窗口的构造函数看起来像这样...
public MainWindow()
{
this .DataContext = new MainViewModel();
InitializeComponent();
myUserControlName.DataContext = new UserControlViewModel();
}
你好 pd1989 ,
你是的,但是,
顺便说一句,它不是一个使用datacontext属性的好方法。在MainViewModel的构造函数中启动UserControlViewModel的更好方法。看看这个,
类 MainViewModel:BaseViewModel
{
public UserControlViewModel UserControlVM { get ; set ;}
public MainViewModel()
{
UserControlVM = new UserControlViewModel();
}
}
并在MainWindow中使用XAML代码,如下所示,
< my:usercontrol grid.column = 0 grid.row = 1 horizontalalignment = 拉伸 verticalalignment = 拉伸 datacontext = {Binding xmlns:my = #unknown > < span class =code-keyword>< / my:usercontrol >
谢谢。
Hi,
I have a User Control in a Window. I need a DataContext for the Window and another one for the UserControl. The problem is that the DataContext from the Window inherits to the DataContext from the User Control. That means, after initializing the application I lost my DataContext from the UserControl, but have the DataContext from the Window at both, Window and UserControl.
When I don''t set the DataContext of the Window then my User Control gets the right one. That means, each DataContext allocation works for it''s own, but not together.
My Xaml for the window:
<Window x:Class="myproj.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:my="clr-namespace:myproj.Controls"
xmlns:myBehaviour="clr-namespace:Behaviour"
Title="window" WindowState="Maximized" myBehaviour:Behaviour.Command="{Binding Command}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="TreeViewColumn" Width="250" />
<ColumnDefinition x:Name="MainColumn" MinWidth="200" />
</Grid.ColumnDefinition>
<Grid.RowDefinitions>
<RowDefinition x:Name="MenuBarRow" Height="56" />
<RowDefinition x:Name="MainRow" MinHeight="200" Height="200*" />
</Grid.RowDefinition>
<my:UserControl Grid.Column="0" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
</Grid>
</Window>
My Code - behind constructor for the window
public MainWindow()
{
this.DataContext = new MainViewModel();
InitializeComponent();
}
My Xaml for the UserControl
<UserControl x:Class="myproj.Controls.myUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" >
<DockPanel>
<TextBox TextWrapping="Wrap" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Text="{Binding myProperty, UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Top" />
</DockPanel>
</UserControl>
My Code - behind constructor for the User Control
public UserControl()
{
InitializeComponent();
this.DataContext = new UserControlViewModel;
}
I hope someone can give me a piece of advice.
Thank you.
Yes, it''s solved,
now my constructor of my mainwindow looks like this...
public MainWindow() { this.DataContext = new MainViewModel(); InitializeComponent(); myUserControlName.DataContext = new UserControlViewModel(); }
Hello pd1989,
You are right but,
By the way its not a good way to use datacontext properties. Better way to initiate UserControlViewModel in MainViewModel''s constructor. Look This,
class MainViewModel : BaseViewModel { public UserControlViewModel UserControlVM {get; set;} public MainViewModel() { UserControlVM = new UserControlViewModel(); } }
and use XAML code in MainWindow like this,
<my:usercontrol grid.column="0" grid.row="1" horizontalalignment="Stretch" verticalalignment="Stretch" datacontext="{Binding" xmlns:my="#unknown"></my:usercontrol>
Thanks.
这篇关于WPF中DataContext的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!