我正在尝试让我的wpf应用程序在加载时占用我的右侧监视器。我的方法涉及创建一个存储屏幕宽度值(作为双精度值)的属性,然后将其绑定到Window.Left属性。但是,无论我做什么,应用程序窗口都不会移到我正确的监视器上。
这是我的xaml
<Window x:Class="Foo.FooView"
.. declarations
Title="{Binding Title}"
Left="{Binding ScreenOffset, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
Height="800"
Width="1920"
SnapsToDevicePixels="True">
在我的视图模型中:
public double ScreenOffset
{
get
{
return _screenOffset;
}
set
{
if (_screenOffset != value)
{
_screenOffset = value;
RaisePropertyChanged(nameof(ScreenOffset));
}
}
}
private double _screenOffset = 0.0;
public FooViewModel()
{
ScreenOffset = Application.Current.MainWindow.Width + defaultOffset;
//Application.Current.MainWindow.Width = 1920
//defaultOffset = 10.0
}
我的数据绑定与我的应用程序中的其他控件一起使用。似乎无效的唯一绑定只是Left属性。谁能看到我在做什么错?提前谢谢了。
答案 0 :(得分:1)
您可以尝试在视图模型中放置以下代码吗?
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public double ScreenOffset
{
get
{
return _screenOffset;
}
set
{
if (_screenOffset != value)
{
_screenOffset = value;
NotifyPropertyChanged();
}
}
}
答案 1 :(得分:0)
首先,我要感谢@PavelAnikhouski和@Miguel Carreira所付出的时间和精力,以帮助我解决这一问题。因此,我的问题与我没有正确设置绑定的事实无关。我的问题是我的 WindowState 从一开始就默认为 Maximized 。 site也帮助我理解了这一点。我通过绑定 WindowState 并将其默认设置为 Normal 来解决了我的问题。然后,我设置新的 Left 值并将其绑定到属性,最后将 WindowState 更改为 Maximized 。
//setting up the dashboard window position, first
//the WindowState has to be set to Normal
WindowState = WindowState.Normal;
//second, we get the offset value based on the configuration settings
ScreenOffset = Application.Current.MainWindow.Width + defaultOffset;
//third, we set the desired WindowState to Maximized
WindowState = WindowState.Maximized;
现在,我的窗口出现在所需位置。再次谢谢你。