nor*_*ndo 8 wpf minimize mvvm windowstate
我将主窗口的"WindowState"属性绑定到我的ViewModel,以便通过命令更改窗口的状态,但是第一次最小化窗口时,它最小化,就像Excel文件中的工作表一样.是否有解决此问题或将"WindowState"属性绑定到我的ViewModel的正确方法,以便窗口最小化?
Bin*_*nil 17
这是使用中继命令逻辑测试的示例工作.您将通过Model-View-ViewModel设计模式获得有关WPF应用程序的更多详细信息.
<Window x:Class="WpfMvvmTestCSharp.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:WpfMvvmTestCSharp"
Title="Window1" Height="300" Width="300" WindowState="{Binding CurWindowState, Mode=TwoWay}">
<Window.DataContext>
<vm:Window1ViewModel/>
</Window.DataContext>
<Grid>
<Button Command="{Binding CmdMax}" Height="23" Margin="12,25,0,0" Name="button1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75">Maximize</Button>
<Button Command="{Binding CmdMin}" Height="23" Margin="101,25,102,0" Name="button2" VerticalAlignment="Top">Minimize</Button>
<Button Command="{Binding CmdRes}" Height="23" HorizontalAlignment="Right" Margin="0,25,13,0" Name="button3" VerticalAlignment="Top" Width="75">Restore</Button>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
并在Windows ViewModel中
class Window1ViewModel:ViewModelBase
{
public Window1ViewModel()
{
CurWindowState = WindowState.Maximized;
}
public ICommand CmdMax
{
get { return new RelayCommand(param => onCmdMax()); }
}
void onCmdMax()
{
CurWindowState = WindowState.Maximized;
}
public ICommand CmdMin
{
get { return new RelayCommand(param => onCmdMin()); }
}
void onCmdMin()
{
CurWindowState = WindowState.Minimized;
}
public ICommand CmdRes
{
get { return new RelayCommand(param => onCmdRes()); }
}
void onCmdRes()
{
CurWindowState = WindowState.Normal;
}
private WindowState _curWindowState;
public WindowState CurWindowState
{
get
{
return _curWindowState;
}
set
{
_curWindowState = value;
base.OnPropertyChanged("CurWindowState");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不认为你应该关心视图模型中的窗口状态,这是完全错误的,因为较低级别的层知道更高级别的层(因此错误的关注点(SOC)).
在这种情况下,我通常做的是从包含视图模型的控件或窗口(因此视图)的代码隐藏中订阅视图模型中的更改.在这种情况下,在代码隐藏中编写代码是有效的,因为它只在视图中使用(因此代码隐藏是这个逻辑的完美位置,你真的不想进行单元测试).
| 归档时间: |
|
| 查看次数: |
14265 次 |
| 最近记录: |