c# - 窗口的WPF MaxWidth绑定不起作用
问题描述
我试图绑定我的 Maxwidth Window
而不是在 xaml 代码中设置它。
之前是:
<Window x:Class="WpfDialogs.GenericWindow"
x:Name="BaseDialog"
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"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
d:DataContext="{d:DesignInstance Type=wpfDialogs:DialogViewModel, IsDesignTimeCreatable=True}"
Title="{Binding Title}"
SizeToContent="WidthAndHeight"
MaxWidth="1280" MaxHeight="600"
MinWidth="400" MinHeight="400"
WindowStyle="ToolWindow"
ResizeMode="CanResizeWithGrip">
我将 xaml 更改为
MaxWidth="{Binding MaximumWidth, Mode=TwoWay}" MaxHeight="{Binding MaximumHeight, Mode=TwoWay}"
并添加到虚拟机:
public double MaximumHeight
{
get => mMaxHeight;
set { mMaxHeight = value; }
}
public double MaximumWidth
{
get => mMaxWidth;
set { mMaxWidth = value; }
}
虚拟机已创建,DataContext
并将Window
设置为虚拟机。
setter 和 getter 都被调用(在 VM 的构造函数中设置并在创建视图时获取)但是在测试这个时,我可以尽可能多地最大化窗口。
我错过了什么?谢谢
解决方案
您的属性未实现INotifyPropertyChanged
,因此更改不会反映在 UI 中。
public class YourViewModel : INotifyPropertyChanged
{
// ...other view model code.
private double mMaxHeight;
private double mMaxWidth;
public double MaximumHeight
{
get => mMaxHeight;
set
{
mMaxHeight = value;
OnPropertyChanged();
}
}
public double MaximumWidth
{
get => mMaxWidth;
set
{
mMaxWidth = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
推荐阅读
- navigation - 将基于页面的导航中的初始视图控制器设置为第一个视图以外的其他内容
- python - 使用 BeautifulSoup 删除具有特定 id 的 div 时出错
- jquery - 对 Slack URL 的 AJAX 调用在 Safari 浏览器上不起作用
- c++ - 是否可以更改地图中的一对?
- python - PyTorch 中 tensor.permute 和 tensor.view 的区别?
- c# - 运行 SqlQuery EF 时 FieldNameLookup.GetOrdinal 中的索引超出范围异常
- python - 批量收集/GatherND
- docker - 我需要 Docker for Kubernetes 吗?
- microservices - 在微服务中不复制数据的“现实世界”解决方案是什么?
- c# - 单击另一个 UserControl 上的按钮后动态加载 UserControl