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.
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>
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.
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).