WPF 将 WindowState 绑定到 UserControl 中的命令
WPF Bind WindowState to Command in UserControl
我正在尝试制作用于 WPF 应用程序的自定义标题栏,部分原因是为了学习/练习使用用户控件。我已经完成了我认为我需要做的将 usercontrol 视图模型中的命令绑定到主窗口的 属性 (窗口状态)并按下按钮,但是尽管按下按钮时命令正在执行, 包含用户控件的主窗口的窗口状态不会改变。
这里的绑定是不是有什么不明白的地方?我觉得我已经完全按照我能在网上找到的所有说明进行操作了! (显然我没有)
这是用户控件 xaml:
<UserControl x:Class="UserControls.TitleBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:ViewModels"
mc:Ignorable="d">
<UserControl.DataContext>
<vm:TitleBar_ViewModel/>
</UserControl.DataContext>
<Button Command="{Binding MaximiseCommand}" />
</UserControl>
视图模型中的代码如下所示:
namespace ViewModels
{
public class TitleBar_ViewModel : DependencyObject, INotifyPropertyChanged
{
public TitleBar_ViewModel() { }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private WindowState _windowState;
public WindowState WindowState
{
get
{ return _windowState; }
set
{
_windowState = value;
OnPropertyChanged();
}
}
private ICommand _maximiseCommand;
public ICommand MaximiseCommand
{
get
{
if (_maximiseCommand == null)
{
_maximiseCommand = new RelayCommand(param => this.Maximise(), param => true);
}
return _maximiseCommand;
}
}
private void Maximise()
{
WindowState = WindowState.Maximized;
}
}
}
使用用户控件的主窗口xaml如下:
<Window x:Class="Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:uc="clr-namespace:UserControls;assembly=UserControls"
mc:Ignorable="d"
WindowState="{Binding WindowState,ElementName=Titlebar}">
<Grid>
<uc:TitleBar x:Name="Titlebar"/>
</Grid>
</Window>
在本次演示中,我尽量保持简单,因此希望您能够就我遗漏的构建过程的某些重要部分提供一些指示,这些部分导致主窗口窗口状态不受影响通过按下按钮。
任何指点都会很棒;这种用户控件的使用真的让我很吃力,我真的很想开始使用它来避免为每个应用程序一遍又一遍地复制代码!
您的 Usercontrol
没有 WindowState
属性,但是 DataContext
。
所以:
WindowState="{Binding DataContext.WindowState,ElementName=Titlebar}"
我正在尝试制作用于 WPF 应用程序的自定义标题栏,部分原因是为了学习/练习使用用户控件。我已经完成了我认为我需要做的将 usercontrol 视图模型中的命令绑定到主窗口的 属性 (窗口状态)并按下按钮,但是尽管按下按钮时命令正在执行, 包含用户控件的主窗口的窗口状态不会改变。
这里的绑定是不是有什么不明白的地方?我觉得我已经完全按照我能在网上找到的所有说明进行操作了! (显然我没有)
这是用户控件 xaml:
<UserControl x:Class="UserControls.TitleBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:ViewModels"
mc:Ignorable="d">
<UserControl.DataContext>
<vm:TitleBar_ViewModel/>
</UserControl.DataContext>
<Button Command="{Binding MaximiseCommand}" />
</UserControl>
视图模型中的代码如下所示:
namespace ViewModels
{
public class TitleBar_ViewModel : DependencyObject, INotifyPropertyChanged
{
public TitleBar_ViewModel() { }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private WindowState _windowState;
public WindowState WindowState
{
get
{ return _windowState; }
set
{
_windowState = value;
OnPropertyChanged();
}
}
private ICommand _maximiseCommand;
public ICommand MaximiseCommand
{
get
{
if (_maximiseCommand == null)
{
_maximiseCommand = new RelayCommand(param => this.Maximise(), param => true);
}
return _maximiseCommand;
}
}
private void Maximise()
{
WindowState = WindowState.Maximized;
}
}
}
使用用户控件的主窗口xaml如下:
<Window x:Class="Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:uc="clr-namespace:UserControls;assembly=UserControls"
mc:Ignorable="d"
WindowState="{Binding WindowState,ElementName=Titlebar}">
<Grid>
<uc:TitleBar x:Name="Titlebar"/>
</Grid>
</Window>
在本次演示中,我尽量保持简单,因此希望您能够就我遗漏的构建过程的某些重要部分提供一些指示,这些部分导致主窗口窗口状态不受影响通过按下按钮。
任何指点都会很棒;这种用户控件的使用真的让我很吃力,我真的很想开始使用它来避免为每个应用程序一遍又一遍地复制代码!
您的 Usercontrol
没有 WindowState
属性,但是 DataContext
。
所以:
WindowState="{Binding DataContext.WindowState,ElementName=Titlebar}"