Bind a textblock to static property which isn'

2019-09-02 05:11发布

问题:

This is my xaml code:

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

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>

How do I do this?

回答1:

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}"/>