Search code examples
c#wpf

How to bind a const string from a class to textblock


For a textblock i need to set a text from a class that has string.

Instead of giving the text directly to text property trying to set from different class.tried binding by path and passing the field but gives attachable issue. what is the correct approach to set a text from different class to a textblock?


Solution

  • If the string is defined as const then you don't need data binding as the value will never change.

    Besides that, you can't bind to fields in WPF. You would have to create a static property if binding is the goal.

    If the field is public and static (or a constant) you can use the x:Static markup extension:

    <TextBlock Text="{x:Static MyClass.MyConstField}" />