Reputation: 743
I am trying to get my wpf application to take up my right-side monitor when it loads. My approach involves me creating a property that stores the screen width value (as a double) and then bind this to the Window.Left property. But no matter what I do the application window doesn't move to my right monitor.
here is my xaml
<Window x:Class="Foo.FooView"
.. declarations
Title="{Binding Title}"
Left="{Binding ScreenOffset, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
Height="800"
Width="1920"
SnapsToDevicePixels="True">
in my View Model:
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
}
My data binding works with other controls in my application. The only binding that doesn't seem to work is just the Left property. Can anyone see what I am doing wrong? Many thanks in advance.
Upvotes: 1
Views: 1352
Reputation: 743
First I want to thank @PavelAnikhouski and @Miguel Carreira for your time and effort to help me figure this out. So, my problem had nothing to do with the fact that I wasn't setting up my binding properly. My issue was that my WindowState was defaulted to Maximized from the start. This site helped me understand that too. I solved my issue by binding my WindowState and defaulting it to Normal. I then set the new Left value and bind it to the property, and finally I changed my WindowState to 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;
Now my window appears in its desired location. Thank you again.
Upvotes: 1
Reputation: 526
Can you try to put the following code in your view model:
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();
}
}
}
Upvotes: 1