Best way to bind WPF properties to ApplicationSettings in C#?

后端 未结 7 704
野的像风
野的像风 2020-12-07 15:40

What is the best way to bind WPF properties to ApplicationSettings in C#? Is there an automatic way like in a Windows Forms Application? Similar to this question, how (and

7条回答
  •  离开以前
    2020-12-07 16:32

    You can directly bind to the static object created by Visual Studio.

    In your windows declaration add:

    xmlns:p="clr-namespace:UserSettings.Properties"
    

    where UserSettings is the application namespace.

    Then you can add a binding to the correct setting:

    
    

    Now you can save the settings, per example when you close your application:

    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    {
        Properties.Settings.Default.Save();
        base.OnClosing(e); 
    }
    

提交回复
热议问题