将文本框绑定到不在VM中的静态属性 [英] Bind a textblock to static property which isn't in the VM

查看:130
本文介绍了将文本框绑定到不在VM中的静态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的xaml代码:

This is my xaml code:

<TextBlock Text="{Binding MyTranslations[0].Name}"></TextBlock>

我想做的是删除0.而不是0,我需要得到正确的整数来自静态类的静态字段,它位于另一个项目中,但是在samo解决方案中。

What I want to do is remove the 0. Instead of 0, I need to get the correct integer from a static field in a static class which is in another project but in the samo solution.

我猜应该看起来像这样:

I guess it should look like something like this:

<TextBlock Text="{Binding MyTranslations[MyStaticClass.MyStaticInt].Name}"></TextBlock>

我该怎么做?

推荐答案

可能有一些奇怪的方式在xaml中进行语法分析,但是通常当我遇到这样的奇怪问题时,通常只需要在VM中创建一个计算属性。

There is probably some strange way to do this syntactically in xaml, but usually when I come across strange problems like this, I usually just make a calculated property in my VM.

public string MyCurrentTranslation
{
    get { return MyTranslations[MyStaticClass.MyStaticInt].Name; }
}

然后只需绑定到该属性:

Then just bind to that property:

<TextBlock Text="{Binding MyCurrentTranslation}"/>

这篇关于将文本框绑定到不在VM中的静态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆