Wpf 如何为不同的数据模板设置窗口位置
如何根据窗口当前显示的视图设置主窗口的位置 我使用DataTemplates选择正确的视图,并将其作为内容实现到MainWIndow 例如Wpf 如何为不同的数据模板设置窗口位置,wpf,vb.net,mvvm,Wpf,Vb.net,Mvvm,如何根据窗口当前显示的视图设置主窗口的位置 我使用DataTemplates选择正确的视图,并将其作为内容实现到MainWIndow 例如 <ContentControl> <ContentControl.Style> <Style TargetType="{x:Type ContentControl}"> <Style.Triggers>
<ContentControl>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding LoginViewM.Content}" Value="">
<Setter Property="Content">
<Setter.Value>
<SP:SplashViewModel/>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding LoginViewM.Content}" Value="Admin">
<Setter Property="Content">
<Setter.Value>
<EE:EmployeeViewModel/>
</Setter.Value>
</Setter>
</DataTrigger>
非常感谢您的建议。您可以将
主窗口
绑定到视图模型中的宽度
和高度
属性。以下是一个例子:
private double _WindowWidth = 500;
public double WindowWidth
{
get { return _WindowWidth; }
set
{
_WindowWidth = value;
//INotifyPropertyChanged stuff.
OnPropertyChanged();
}
}
在您的视图中
:
Width="{Binding WindowWidth}"
使用WindowWidth
属性,您可以为Left
和Top
设置另一个属性
private double _WindowLeft = 150;
public double WindowLeft
{
get { return _WindowLeft ; }
set
{
_WindowLeft = value;
//INotifyPropertyChanged stuff.
OnPropertyChanged();
}
}
与前面一样,您可以绑定到属性:
Left="{Binding WindowLeft}"
此方法的好处是,您现在可以访问视图模型中的窗口左
、顶
、宽
、和高
属性。因此,您可以执行逻辑,调整/重新定位构造函数中的视图,或视图模型中需要的位置
考虑将这些属性放在基类中,因此所有视图模型都可以从该类继承,而无需实现这些属性
Left="{Binding WindowLeft}"