[英]UserControl inside UserControl is not reflecting data from ViewModel
In MainWindow, I am setting its contant to be a UserControl - 在MainWindow中,我将其内容设置为UserControl-
<Grid>
<local:MainControl></local:MainControl>
</Grid>
In MainControl, I am using other UserControls - 在MainControl中,我正在使用其他UserControls-
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="White" Offset="0"></GradientStop>
<GradientStop Color="LightBlue" Offset="1"></GradientStop>
</LinearGradientBrush>
</Grid.Background>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
</Grid>
<Grid Grid.Row="0">
<ui:TestHeader Height="20" Padding="20" Margin="2" Background="Gray"></ui:TestHeader>
</Grid>
</Grid>
Here TestHeader is a simple UserControl - 这里的TestHeader是一个简单的UserControl-
<UserControl.DataContext>
<model:TestViewModel></model:TestViewModel>
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="{Binding Path=Test}"/>
</Grid>
Now I am able to see the data comming from ViewModel, when I use TestHeader directly in MainWindow, However when I use the same TestHeader in MainControl, its not getting data from ViewModel. 现在,当我直接在MainWindow中使用TestHeader时,我就能看到来自ViewModel的数据,但是当我在MainControl中使用相同的TestHeader时,它不会从ViewModel获取数据。 How can I tackle this. 我该如何解决。 I think, I missed something, but don't know what. 我想,我错过了一些东西,但不知道是什么。
Please suggest the solution. 请提出解决方案。 Thanks in Advance. 提前致谢。
Update 更新资料
I found this - 我找到了这个 -
<Grid Grid.Row="1">
<ui:TestHeader Height="20" Padding="20" Margin="2" Background="Gray"></ui:TestHeader>
</Grid>
<ui:TestHeader Grid.Row="0" Background="Gray" Height="20"></ui:TestHeader>
here, whole contant is in Grid of MainWindow. 在这里,整个内容位于MainWindow的Grid中。 The outer TestHeader is populating the data, however, the TestHeader inside the Grid is not. 外部的TestHeader正在填充数据,但是,网格内部的TestHeader没有填充数据。
Update 更新资料
I got my silliest mistake ever - 我犯了最严重的错误-
See the padding and height, both are 20. There is no space for contant. 请参见填充和高度,两者均为20。没有余量。
您需要在父用户控件上设置DataContext
(设置为ViewModel),然后使用ViewModel的Properties将其绑定到控件的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.