设置的动态绑定?

发布于 2024-10-01 12:03:46 字数 554 浏览 1 评论 0原文

我面临一个问题,我有一个具有多个设置文件的应用程序,但它们具有完全相同的字段。

示例:

  • Profil1.settings
  • Profil2.settings
  • Profil3.settings

我想在设置窗口 XAML 中执行的操作是动态更改每个配置文件的绑定源。

目前,我的 XAML 绑定如下所示:

SelectedValue="{Binding Source={x:Static Local:Properties.Profil1.Default}, Path=CurrentProfil, Mode=TwoWay, UpdateSourceTrigger=Explicit}"

我的问题是如何用“Profil2”替换“Profil1”,而不通过后面的代码为每个控件一一重新制作所有绑定?是否可以在 XAML 绑定源中使用一些反射变量,而不是简单地使用 Profil1 类型或 Profil2 类型更改该变量的类型?

任何人都可以帮我解决这个问题吗?

提前致谢。

I am facing a problem, I have a application with several setting's files but which have exactly the same fields.

Sample :

  • Profil1.settings
  • Profil2.settings
  • Profil3.settings

What I'd like to do in my setting window XAML is to dynamicaly change the binding source for each profile.

At the moment my XAML binding look like this :

SelectedValue="{Binding Source={x:Static Local:Properties.Profil1.Default}, Path=CurrentProfil, Mode=TwoWay, UpdateSourceTrigger=Explicit}"

My problem is how to replace the "Profil1" by "Profil2" without remaking all my Binding one by one for each controls via my code behind ? Is that possible to use some king of reflection variable in the XAML binding source and than simply change the type of that variable with Profil1 type or Profil2 type ?

Anyone can help me about this ?

Thank in advance.

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

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

发布评论

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

评论(1

拔了角的鹿 2024-10-08 12:03:46

您要做的是将设置窗口的 DataContext 设置为适当的配置文件。您可以随心所欲地执行此操作,但通过这样做,您的所有绑定都将指向该对象。要使绑定像现在一样工作,您需要执行以下操作:

<Window x:Class="MyNamespace.MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Local="clr-namespace:MyNamespace"
        DataContext="{Binding Source={x:Static Local:Properties.Profil1.Default}}">

    ...

    <ComboBox SelectedValue="{Binding Path=CurrentProfil, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />

What you want to do is set the DataContext of your settings window to the appropriate profile. You can do it anyway you want, but by doing so, all your bindings would point to that object. To make the bindings work like you have now, you'd do:

<Window x:Class="MyNamespace.MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Local="clr-namespace:MyNamespace"
        DataContext="{Binding Source={x:Static Local:Properties.Profil1.Default}}">

    ...

    <ComboBox SelectedValue="{Binding Path=CurrentProfil, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文