WPF window location binding

On Windows forms, there was an option in the form properties section to establish a binding between the application setting and the window form.

I usually get a parameter called frmMyFormName_Location, which is then automatically updated as needed, and all I had to do was call the Settings.Save () method when the application exited to save the location.

Can someone please give an example of the same in WPF since I was not able to figure out how to do this?

+4
source share
3 answers

It is very easy to link user or application settings to a .settings file in WPF.

Here is an example of a window that gets its position and size from the settings:

 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:settings="clr-namespace:WpfApplication1.Properties" Height="{Binding Height, Source={x:Static settings:Settings.Default}, Mode=TwoWay}" Width="{Binding Width, Source={x:Static settings:Settings.Default}, Mode=TwoWay}" Top="{Binding Top, Source={x:Static settings:Settings.Default}, Mode=TwoWay}" Left="{Binding Left, Source={x:Static settings:Settings.Default}, Mode=TwoWay}"> <Grid> </Grid> </Window> 

The settings are as follows:

Settings file

And to persist, I just use the following code:

 void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { Properties.Settings.Default.Save(); } 
+19
source

And here is an example of WPF VB.NET

 <Window x:Class="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:local="clr-namespace:WpfApplication1" xmlns:Properties="clr-namespace:WpfApplication1" Title="Test" Loaded="Window_Loaded" Closing="Window_Closing" Height="{Binding Height, Source={x:Static Properties:MySettings.Default}, Mode=TwoWay}" Width="{Binding Width,Source={x:Static Properties:MySettings.Default}, Mode=TwoWay}" Left="{Binding Left,Source={x:Static Properties:MySettings.Default}, Mode=TwoWay}" Top="{Binding Top, Source={x:Static Properties:MySettings.Default}, Mode=TwoWay}" > <Grid Name="MainFormGrid"> ... 
+1
source

The links below can help save the application settings. There is no single "Location" property in the WPF window, but you do have a LocationChanged event that you can handle and write accordingly.

A rough example:

 private void Window_LocationChanged(object sender, EventArgs e) { var left = (double)GetValue(Window1.LeftProperty); var top = (double)GetValue(Window1.TopProperty); // persist these values . . . } 

For permanent application settings:

C # - approach for saving custom settings in a WPF application? Settings-in-MOF applications

WPF Application Settings File

Where to store general application settings

0
source

Source: https://habr.com/ru/post/1344525/


All Articles