Question

Scenario

I have a class which looks something like this:

class GlobalAssemblyInfo
{
    public const string AssemblyName = "MyAppName";
}

The class is NOT contained in a namespace.

I tried unsuccessfully to bind to this using the following code:

Text="{Binding GlobalAssemblyInfo.AssemblyName}"

 

Question

Is this possible, and if so, how would I accomplish this?

 

Why I Need This

Before I get any comments about only using bindings for dynamically changing content, let me explain why I need this.

I am creating an app which may need to undergo a rename due to trademark issues. I need to use the name in several places, such as "MyApp Contributors" or "MyApp Help." Using a binding would enable me to say merely Text="{Binding AppName, StringFormat='{}{0} Contributors'}".

Was it helpful?

Solution

You have two problems here. The first is that the way you're using the class in the binding is assigning it to the Path so it is looking for a property named GlobalAssemblyInfo on whatever your DataContext is. You instead need to use a static source and also expose the value as a property instead of a const. This uses the form:

{Binding Source={x:Static ns:Class.StaticProperty}}

The other problem is that you have no namespace to create your xmlns from in XAML. You should consider if you really need the class to not be namespaced but if you do you should be able to use the weird construction of

xmlns:myGlobal="clr-namespace:;assembly="

where you would then use myGlobal in the place of ns in the first example.

OTHER TIPS

If you created a class that has no namespace, why don't you just put your assembly name in Resources in XAML or Resources in project so you can do binding in it with StringFormat

In your App.xaml.

Define something like this

<x:String x:Key="AssemblyName">Your Value</x:String>

or add a new resource file in your project and define it there.

So you can use it something like this

Text="{Binding Source={StaticResource AssemblyName}, Path=., StringFormat="{}}"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top