0

I have a class in assembly "AssemblyX" with property "Comment". I want to bind AssemblyX.Comment to TextBlock.Text in another assembly?

I'm trying to do it in the following way but it is not working.

<Window xmlns:cc="clr-namespace:SomeNamespace;assembly=AssemblyX">
<TextBlock Text={Binding cc:Comment}/>
...
John
  • 307
  • 1
  • 4
  • 15

3 Answers3

6

You usually don't bind to a property of a class, you bind to a property of an instance of a class. So in your codebehind you'd create an instance:

SomeNamespace.SomeClass instance = new SomeClass();
instance.Comment = "bla";
this.DataContext = intstance;

And in your xaml you bind:

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

In this case it absolutely does not matter in what assembly SomeClass is declared, as long as you current project references that assembly. It also doesn't matter what SomeClass is named. All that matters is that the instance you bind against has a property named Comment.

If the property of your class is static and you therefore don't have an instance, you can bind to the static property like this:

<TextBlock Text="{Binding cc:SomeClass.Comment}"/> 
bitbonk
  • 48,890
  • 37
  • 186
  • 278
0

To bind to a static property of a class ( static Command maybe ) try this

<MenuItem Header="{x:Static SomeClass.SomeProperty}"/>

Code behind

public class SomeClass
{
    public static string SomePropety 
    { get { return "done"; } }
}
shakram02
  • 10,812
  • 4
  • 22
  • 21
0

if your class is not static, you have to create an instance for your class. then you can bind to a property.

look here maybe it helps you

Community
  • 1
  • 1
blindmeis
  • 22,175
  • 7
  • 55
  • 74