如何从xaml访问MyProject.MySettings.Default.SomeSetting?

发布于 2024-08-29 14:44:27 字数 645 浏览 3 评论 0原文

我在项目设置中定义了一个 StringCollection。 我想使用组合框中的值。

有没有办法访问它xamly?

我尝试过:

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

****src** 是项目的 xmlns*

它说:“未找到类型 src:MySettings.Default”。

问题是 MySettings 是一个提供 Default 属性的类,该属性是 MySettings 的线程安全实例,我真的想从 Default 属性获取集合,而不是通过实例化新的属性。

还有其他我不知道的方法吗,也许 ObjectDataProvider 可以访问静态对象?

我想,也许我可以在 App.xaml 中创建一个返回 MySettings.Default 的全局资源,它是 MySettings 类的实例,然后访问它的所有属性,我会尝试一下,但我更喜欢简单的方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔女人霸气范 2024-09-05 14:44:27

我一直使用 x:Static 标记扩展 来完成此操作。关键是将源设置为 Settings.Default 以及所需设置的路径,如下所示:

<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'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>
骄傲 2024-09-05 14:44:27

作为 Joseph 答案的扩展,还可以通过使用静态包装类直接使用 {x:Static } 访问各个设置。这允许使用绑定不起作用的设置值 - 最明显的是动画或不是依赖属性的属性。

基本方法是:

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

然后,您可以在包装类所在的命名空间下的 XAML 中按照 的行引用它。

作为对此的补充,尝试直接公开 Default 是行不通的,大概是因为它不够静态——Default 上的属性是实例属性,而 {x:Static} 则不然。不喜欢。不过,您可能可以编写一个自定义标记扩展来获取实例属性。

这对于基于资源的动画方法特别有用,严格来说,您不想绑定(这会带来各种可冻结问题),但您也不希望拥有大量单例 50.0s 四处乱窜(或者想要避免合并应用程序级资源字典的开销,我认为这是不只是在应用程序中实例化一次)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文