在 WPF 中的 xaml 代码隐藏中绑定 const 成员

发布于 2024-10-22 20:46:03 字数 1252 浏览 1 评论 0原文

有没有什么好方法可以将属性绑定到代码隐藏中的 const 值?

当我使用 ComboBox 时,我通常在 xaml 和代码隐藏中这样做:

XAML:

<ComboBox Name="cbBuz">
   <ComboBoxItem Content="foo" Uid="foo" IsSelected="true" />
   <ComboBoxItem Content="bar" Uid="bar" />
</ComboBox>

Codebehind:

ComboBoxItem item = cbBuz.GetSelectedItem();
switch (item.Uid)
{
    case "foo":  ... break;
    case "bar":  ... break;
}

我选择这种方式的原因如下:

  • 出于本地化目的,内容字符串不应用于确定在保存和加载期间选择哪个项目最后选择的项目。
  • 为了简单起见,XAML 和代码隐藏应该连接内部标识符(在本例中为 Uid)。这样,XAML 和代码隐藏就可以分开维护。

然而,从维护角度来看,内部标识符应该像这样在一处定义:

//IDs
public const string ID_foo = "foo";
public const string ID_bar = "bar";

...

//
switch (item.Uid)
{
    case ID_foo:  ... break;
    case ID_bar:  ... break;
}

问题是看似属性不能是 const 值,因此无法将 ID_foo 和 ID_bar 绑定到 ComboBoxItem 的 Uid,如下所示:

//If ID_foo and ID_bar are properties, this will work.
<ComboBox Name="cbBuz">
   <ComboBoxItem Content="foo" Uid="{Binding ID_foo}" IsSelected="true" />
   <ComboBoxItem Content="bar" Uid="{Binding ID_bar}" />
</ComboBox>

所以,我想知道如何解决这个问题。 或者说有没有更好的实现方法。这也很好。

最好的,

Is there any good way to bind a property to a const value in codebehind?

When I use ComboBox, I usually do this way in xaml and code behind:

XAML:

<ComboBox Name="cbBuz">
   <ComboBoxItem Content="foo" Uid="foo" IsSelected="true" />
   <ComboBoxItem Content="bar" Uid="bar" />
</ComboBox>

Codebehind:

ComboBoxItem item = cbBuz.GetSelectedItem();
switch (item.Uid)
{
    case "foo":  ... break;
    case "bar":  ... break;
}

The reason why I chose this way is following:

  • For localization purpose, Content string should not be used to determine which item is selected during saving and loading a last selected item.
  • For simplicity, XAML and code-behind should be connected internal identifier (In this case, Uid). So that, XAML and Code-behind can be maintained separately.

However, maintenance-wise, the internal identifier should be defined in one-place like this:

//IDs
public const string ID_foo = "foo";
public const string ID_bar = "bar";

...

//
switch (item.Uid)
{
    case ID_foo:  ... break;
    case ID_bar:  ... break;
}

The problem is seemingly property cannot be const value, so there's no way to bind ID_foo and ID_bar to Uid of ComboBoxItem like this:

//If ID_foo and ID_bar are properties, this will work.
<ComboBox Name="cbBuz">
   <ComboBoxItem Content="foo" Uid="{Binding ID_foo}" IsSelected="true" />
   <ComboBoxItem Content="bar" Uid="{Binding ID_bar}" />
</ComboBox>

So, I want to know how to solve this issue.
Or, is there any better way to implement it. It would be nice, too.

Best,

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

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

发布评论

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

评论(2

苏辞 2024-10-29 20:46:03

您最好使用 StaticExtension,像这样:

Uid="{x:Static local:YourClass.ID_foo}"

其中 local 是类的 C# 命名空间的 xmlns 别名。可以在此处找到更多信息。

使用 Binding 的问题是你为永远不会改变的东西增加了很多开销。绑定将尝试监视您的财产。此外,在对象上使用具有非依赖属性的 Binding 时存在已知的“泄漏”不实现 INotifyPropertyChanged。

You would be better off using the StaticExtension, like so:

Uid="{x:Static local:YourClass.ID_foo}"

Where local is an xmlns alias for the C# namespace of your class. More information can be found here.

The problem with using Binding is you are adding a lot overhead for something that will never change. The binding will attempt to monitor your property. Also, there are known "leaks" with using a Binding with a non-dependency property on an object that don't implement INotifyPropertyChanged.

柠檬色的秋千 2024-10-29 20:46:03

您需要创建一个返回常量(在 const 中定义)的属性,即:

private const string ID_Foo = "foo";
public string FooId
{
   get { return ID_Foo; }
}

一旦它位于属性中,就可以通过绑定使用它。

You need to make a property that returns the constant (defined in a const), ie:

private const string ID_Foo = "foo";
public string FooId
{
   get { return ID_Foo; }
}

Once this is in a property, it will be usable via binding.

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