在代码隐藏中使用 Multibinding.StringFormat

发布于 2024-09-26 12:54:36 字数 1114 浏览 3 评论 0原文

我有一个内置动态语言切换的应用程序。根据所选的文化,整个应用程序中的字符串将会改变。翻译后的字符串及其原始值来自资源文件。我使用绑定将资源值附加到按钮、标签等。大多数绑定发生在后面的代码中。

我已经能够使用 Binding.StringFormat 属性将本地化字符串与数据连接起来:

mybinding.StringFormat = "# {0}";

“# of items”。我的问题是我现在需要在后面的代码中连接两个(或更多)本地化字符串。我很快意识到我可以使用 MultiBinding 并向其添加绑定,与当前的工作方式保持一致,但是,使用 MultiBinding.StringFormat 似乎不起作用。我尝试使用 myMultiBinding.StringFormat = "{0} {1}"; 在两个绑定值之间插入空格,但在绑定到“Greetings”标签时它只是显示为空白。

Binding b = GetBinding("HelloWorld");   
Binding b2 = GetBinding("Name");

MultiBinding multib = new MultiBinding();
multib.StringFormat = "{0} {1}";
multib.Bindings.Add(b);
multib.Bindings.Add(b2);
Greetings.SetBinding(Label.ContentProperty, multib);

这里是 GetBinding() 函数,它根据路径值获取绑定:

public Binding GetBinding(string name)
{
    Binding binding = new Binding();
    binding.Source = mySource;
    binding.Path = new PropertyPath(name);
    return binding;
}

另外,我应该注意我正在使用 .NET 4。似乎在 Xaml 中也不起作用。我也在 .NET 3.5 中尝试过这个方法,因为它在 4.0 中不起作用。两个子绑定都在工作...如果我提供转换器,这些值就会显示。不过,我宁愿使用 StringFormat 属性。

I have this application that has dynamic language switching built in. Based on the selected Culture, strings throughout the application will change. Translated strings and their original values come out of resource files. I use bindings to attach the resource values to buttons, labels, etc. Most of this binding occurs in the code behind.

I've been able to concatenate localized strings with data using the Binding.StringFormat property:

mybinding.StringFormat = "# {0}";

for "# of items". My problem is that I now need to concatenate two (or more) localized strings in the code behind. I quickly realized I could use MultiBinding and add my bindings to it, keeping with how things currently work, however, using MultiBinding.StringFormat doesn't seem to work. I'm trying to use myMultiBinding.StringFormat = "{0} {1}"; to insert a space between the two binding values, but it just appears blank when bound to the "Greetings" Label.

Binding b = GetBinding("HelloWorld");   
Binding b2 = GetBinding("Name");

MultiBinding multib = new MultiBinding();
multib.StringFormat = "{0} {1}";
multib.Bindings.Add(b);
multib.Bindings.Add(b2);
Greetings.SetBinding(Label.ContentProperty, multib);

and here's the GetBinding() function which grabs a binding based on the path value:

public Binding GetBinding(string name)
{
    Binding binding = new Binding();
    binding.Source = mySource;
    binding.Path = new PropertyPath(name);
    return binding;
}

Also, I should note I'm using .NET 4. Doesn't seem to work in Xaml either. I have also tried this in .NET 3.5 after it didn't work in 4.0. Both child bindings are working... if I supply a converter, the values show up. I'd rather use the StringFormat property though.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文