作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的 view.xaml :
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=".5*"/>
<RowDefinition Height="0.5*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" x:Name="grdFormSearch" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<local:StudentUserControl HorizontalAlignment="Stretch" Height="100" VerticalAlignment="Stretch"/>
</Grid>
</Grid>
StudentUserControl
。
public partial class StudentUserControl : UserControl
{
public StudentUserControl(StudentViewModel ViewModel)
{
InitializeComponent();
this.DataContext = ViewModel;
}
}
public StudentViewModel(IEventAggregator eventAggregator, IUnityContainer container)
{
_eventAggregator = eventAggregator;
_container = container;
}
DataContext
的
UserControl
?
最佳答案
从 View 的构造函数中删除参数:
public partial class StudentUserControl : UserControl
{
public StudentUserControl()
{
InitializeComponent();
}
}
<local:StudentUserControl HorizontalAlignment="Stretch" Height="100" VerticalAlignment="Stretch"/>
DataContext
。在大多数情况下,
DataContext
应该从父元素(即您的案例)中的view.xaml继承,并且如果您在 View 的构造函数中显式设置DataContext,则会破坏继承。
DataContext
,则可以使用Prism的 View 模型定位器来创建 View 模型:
<UserControl x:Class="WpfApplication1.StudentUserControl"
...
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True">
关于wpf - 如何为 Prism WPF设置用户控件的数据上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44490038/
我是一名优秀的程序员,十分优秀!