c# - WPF 窗口大小绑定
问题描述
我正在尝试将窗口高度调整为分辨率较低的屏幕。由于项目的设计,这个窗口是从 ViewModel 调用的(尽管它的内容是用 XAML 编写的)。因此,为了实现这种调整,我在 ViewModel 中进行了以下操作:
//_window is sort of singleton within the ViewModel, which is a true singleton
private static Window _window;
在方法中,调用_window.Show()
:
//_instance is an instance of the ViewModel. Font size is also adjusted.
Binding fontSizeBinding = new Binding(nameof(FontSize));
fontSizeBinding.Source = _instance;
Binding windowHeightBinding = new Binding(nameof(WindowHeight))
{
Source = _instance,
Mode = BindingMode.TwoWay
};
_window.SetBinding(Window.FontSizeProperty, fontSizeBinding);
_window.SetBinding(Window.HeightProperty, windowHeightBinding);
_window.LocationChanged += _instance._window_LocationChanged;
然后,在_window_LocationChanged
我调整大小:
...
// currentScreenHeight is the height of the screen, the window is on
WindowHeight = currentScreenHeight;
...
在移动到具有不同分辨率的其他显示器时,此窗口在调整大小时“悬空”,从以前的更大尺寸快速更改为新的(看起来它在每次移动时都会发生变化,例如每个像素)。一旦我松开鼠标左键,它就会变回原来的大小。
为什么会发生这种情况,如何以编程方式轻松减小窗口的大小?
编辑: 我对我的代码进行了一些更改,因此当窗口移动到另一个屏幕时,高度只更改一次。获取当前监视器的方法的基础取自这里。我已经对其进行了更改,一旦显示更改,它就会调用一个事件:
private Screen _currentScreen;
public Screen CurrentScreen
{
get => _currentScreen;
private set
{
if (_currentScreen == null || _currentScreen.DeviceName != value.DeviceName)
{
_currentScreen = value;
ScreenChangedEventArgs args = new ScreenChangedEventArgs(value,
GetRect(value.Bounds), GetRect(value.WorkingArea), value.Primary,
value.DeviceName );
CurrentScreenChanged?.Invoke(_currentWindow, args);
}
}
}
忽略ScreenChangedEventArgs
仅包含一些字段的 的实现,此方法工作正常,事件触发,其委托传递的方法执行,但仍然,在我移动鼠标后窗口立即恢复到其原始大小。
解决方案
推荐阅读
- codeigniter - 如何重定向或显示自定义错误消息?
- html - 在最后一行包装弹性项目
- php - 如何通过php中的值区分两个数组?
- ruby-on-rails - 安装 Rails 或 gem 更新时出现错误 (Gem::RemoteFetcher::UnknownHostError)
- c# - 图像中的 EMGU CV 调用错误....对象....所有 Visual Studip 项目都是任何 cpu 目标...chaging 目标没有用
- azure - 应用程序能否使用 AD 身份验证而不是连接字符串密钥访问 Azure Cosmos
- django - 如何在 django rest 框架中发布多个字符串的请求?
- c# - 如何在 wpf c# 中编辑列表视图中的选定项目?
- mysql - 如何从具有匹配列的两个不同 SELECT 语句中获取行
- python - 在一组网页问题中查找特定单词