在WP#中将WPF属性绑定到ApplicationSettings的最佳方法?

时间:2008-09-30 19:20:29

标签: c# .net wpf visual-studio

在C#中将WPF属性绑定到ApplicationSettings的最佳方法是什么?是否有像Windows窗体应用程序一样的自动方式?与this question类似,你如何(并且有可能)在WPF中做同样的事情?

7 个答案:

答案 0 :(得分:101)

您可以直接绑定到Visual Studio创建的静态对象。

在你的windows声明中添加:

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

其中UserSettings是应用程序命名空间。

然后您可以将绑定添加到正确的设置:

<TextBlock Height="{Binding Source={x:Static p:Settings.Default}, 
           Path=Height, Mode=TwoWay}" ....... />

现在,您可以在关闭应用程序时按照示例保存设置:

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

答案 1 :(得分:9)

如果您是 VB.Net 开发人员尝试此操作,答案是不同的。

xmlns:p="clr-namespace:ThisApplication"

注意.Properties不存在。


在你的绑定中,它是MySettings.Default,而不是Settings.Default - 因为app.config以不同的方式存储它。

<TextBlock Height={Binding Source={x:Static p:MySettings.Default}, Path=Height, ...

在拔掉我的头发后,我发现了这一点。希望它有所帮助

答案 2 :(得分:7)

我喜欢接受的答案,但我遇到了一个特例。我将我的文本框设置为“只读”,以便我只能在代码中更改它的值。我无法理解为什么这个值没有传播回设置,尽管我将模式设为“TwoWay”。

然后,我发现了这个:http://msdn.microsoft.com/en-us/library/system.windows.data.binding.updatesourcetrigger.aspx

  

默认值为Default,它返回目标依赖项属性的默认UpdateSourceTrigger值。但是,大多数依赖项属性的默认值是PropertyChanged,而 Text属性的默认值为LostFocus

因此,如果您的文本框包含IsReadOnly =“True”属性,则必须将UpdateSourceTrigger = PropertyChanged值添加到Binding语句中:

<TextBox Text={Binding Source={x:Static p:Settings.Default}, Path=myTextSetting, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged} ... />

答案 3 :(得分:5)

最简单的方法是绑定到一个对象,该对象将您的应用程序设置公开为属性,或者将该对象包含为StaticResource并绑定到该对象。

您可以采取的另一个方向是创建自己的Markup Extension,这样您就可以使用PropertyName =“{ApplicationSetting SomeSettingName}”。要创建自定义标记扩展,您需要继承MarkupExtension并使用MarkupExtensionReturnType属性修饰该类。 John Bowen有一个post on creating a custom MarkupExtension可能会让这个过程更加清晰。

答案 4 :(得分:3)

Kris,我不确定这是绑定ApplicationSettings的最佳方式,但这就是我在Witty中的做法。

1)在window / page / usercontrol / container中为要绑定的设置创建依赖项属性。这种情况我有一个用户设置来播放声音。

    public bool PlaySounds
    {
        get { return (bool)GetValue(PlaySoundsProperty); }
        set { SetValue(PlaySoundsProperty, value); }
    }

    public static readonly DependencyProperty PlaySoundsProperty =
        DependencyProperty.Register("PlaySounds", typeof(bool), typeof(Options),
        new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnPlaySoundsChanged)));

    private static void OnPlaySoundsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        Properties.Settings.Default.PlaySounds = (bool)args.NewValue;
        Properties.Settings.Default.Save();
    }

2)在构造函数中,初始化属性值以匹配应用程序设置

      PlaySounds = Properties.Settings.Default.PlaySounds;

3)在XAML中绑定属性

      <CheckBox Content="Play Sounds on new Tweets" x:Name="PlaySoundsCheckBox" IsChecked="{Binding Path=PlaySounds, ElementName=Window, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

您可以下载完整的Witty source以查看其实际效果,或只浏览code for options window

答案 5 :(得分:2)

我喜欢通过ViewModel进行操作,只是在XAML中正常进行绑定

    public Boolean Value
    {
        get
        {
            return Settings.Default.Value;

        }
        set
        {
            Settings.Default.SomeValue= value;
            Settings.Default.Save();
            Notify("SomeValue");
        }
    }

答案 6 :(得分:0)

另请阅读this关于如何在BabySmash中完成的文章

如果您需要更改通知,您只需要用DO支持设置(如Alan的例子)!绑定到POCO设置类也可以使用!