WPF: easiest way to bind to Settings

Most perfect looks an way with MarkupExtension:

using System.Windows.Data;
 
namespace WpfApplication1
{
    public class SettingBindingExtension : Binding
    {
        public SettingBindingExtension()
        {
            Initialize();
        }
 
        public SettingBindingExtension(string path)
            :base(path)
        {
            Initialize();
        }
 
        private void Initialize()
        {
            this.Source = WpfApplication1.Properties.Settings.Default;
            this.Mode = BindingMode.TwoWay;
        }
    }
}
<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:WpfApplication1"
        Title="Window1"
        Height="{my:SettingBinding Height}"
        Width="{my:SettingBinding Width}"
        Left="{my:SettingBinding Left}"
        Top="{my:SettingBinding Top}">

But there are 2 issues with it:
— it doesn’t work with converter;
— autocompletion for Settings properties doesn’t work in xaml in this case

So, I prefer just to use such template, which allows me to add the necessary binding in 5 key strokes:

{Binding $END$, Source={x:Static properties:Settings.Default}, Mode=TwoWay}

Leave a Reply

  

  

  

www.000webhost.com