Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何为不同的数据模板设置窗口位置_Wpf_Vb.net_Mvvm - Fatal编程技术网

Wpf 如何为不同的数据模板设置窗口位置

Wpf 如何为不同的数据模板设置窗口位置,wpf,vb.net,mvvm,Wpf,Vb.net,Mvvm,如何根据窗口当前显示的视图设置主窗口的位置 我使用DataTemplates选择正确的视图,并将其作为内容实现到MainWIndow 例如 <ContentControl> <ContentControl.Style> <Style TargetType="{x:Type ContentControl}"> <Style.Triggers>

如何根据窗口当前显示的视图设置主窗口的位置

我使用DataTemplates选择正确的视图,并将其作为内容实现到MainWIndow

例如

<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}"