How to access MyProject.MySettings.Default.SomeSetting from xaml?

被刻印的时光 ゝ 提交于 2019-12-24 10:35:17

问题


I have defined a StringCollection in the Project Settings. I want to use the values in a ComboBox.

Is there a way to access it xamly?

I tried:

<CollectionViewSource Source="{x:Static src:MySettings.Default.MyCollection}" />
<CollectionViewSource 
    Source="{Binding Source={x:Static src:MySettings.Default.MyCollection}}" />

****src** is the xmlns of the project*

It says: "Type src:MySettings.Default was not found".

The thing is that MySettings is a class that offers a Default property which is a thread-safe instance of MySettings, I really want to get the collection from the Default property and not by instantiating a new on.

Is there other ways I am not aware of, maybe ObjectDataProvider can access static objects?

I thought, maybe I can make in the App.xaml a global resource that return MySettings.Default which is an instance of the MySettings class, and then access all its properties, I will try that out, but I prefer the easy way.


回答1:


I've always done it using the x:Static Markup Extension. The key is to set the source to Settings.Default and the path to the desired setting like so:

<Window x:Class="SettingsBindSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="clr-namespace:SettingsBindSample.Properties"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <CollectionViewSource x:Key="MyItems" 
      Source="{Binding MyCollection, Source={x:Static s:MySettings.Default}}" />
    </Window.Resources>
    <StackPanel>
        <ComboBox ItemsSource="{Binding Source={StaticResource MyItems}}" />
    </StackPanel>
</Window>



回答2:


I found this. Checking out, hope I am not wasting my time...

http://code.msdn.microsoft.com/StaticExExtension

If you have any better ideas, please lemme know.




回答3:


As an extension to Joseph's answer, it is also possible to access individual settings directly with {x:Static } by using a static wrapper class. This enables use of settings values where a Binding won't work - most notably animations, or properties that aren't Dependency Properties.

The basic approach is:

public static class XamlSettings 
{
     public static MySettingsMember { get {return Properties.MySettings.Default.MySettingsMember; }}
}

which you then refer to in XAML under the namespace the wrapper class is in along the lines of <DoubleAnimation From="{x:Static local:XamlSettings.MySettingsMember}" ... />.

As an addendum to this, trying to expose Default directly doesn't work, presumably because it isn't quite static "enough" - properties on Default are instance properties, which {x:Static} doesn't like. You could probably write a custom markup extension that would obtain instance properties, though.

This is particularly useful for a resource-based approach to animation where you don't strictly speaking want to bind (which brings all sorts of freezable issues into play), but nor do you want to have lots of singleton <sys:double x:Key="MyNumber">50.0</sys:double>s kicking around (or want to avoid the overhead of a merged app-level resource dictionary, which I'm led to believe isn't just instantiated once across the app).



来源:https://stackoverflow.com/questions/2688011/how-to-access-myproject-mysettings-default-somesetting-from-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!