locked
Binding Source RRS feed

  • Question

  • User364376 posted

    Hi,

    I want to set a string in another class say App.Xaml.cs

    public string MyValue = "somevalue";
    

    How can i bind the text of a label to that MyValue item? I'm not sure how the Databinding source works or how I need to wire it up.

    I've tried this which didnt work...

    <Label Text = {Binding MyValue, Source={x:Static local:App.MyValue}} />
    

    I've got a static piece of text that I want to display on multiple pages so thought to put it in one place and then try and bind to that so that when I need to change it i can change it in one place.

    Monday, August 3, 2020 8:56 PM

Answers

  • User369978 posted

    First , we need to make MyValue to be static in App .

    public  static string MyValue = "somevalue";
    

    Then modify the binding in xaml

    <Label Text ="{x:Static local:App.MyValue}" />
    

    Refer to

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/xaml-markup-extensions#the-xstatic-markup-extension

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, August 4, 2020 2:17 AM

All replies

  • User53115 posted

    I would define it as a resource in your App.xaml file and use it via the StaticResource markup extension.

    ``` x:Class="MyNamespace.MyApp"> somevalue

    Monday, August 3, 2020 10:41 PM
  • User369978 posted

    First , we need to make MyValue to be static in App .

    public  static string MyValue = "somevalue";
    

    Then modify the binding in xaml

    <Label Text ="{x:Static local:App.MyValue}" />
    

    Refer to

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/xaml-markup-extensions#the-xstatic-markup-extension

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, August 4, 2020 2:17 AM
  • User364376 posted

    @ColeX said: First , we need to make MyValue to be static in App .

    public static string MyValue = "somevalue";

    Then modify the binding in xaml

    <Label Text ="{x:Static local:App.MyValue}" />
    

    Refer to

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/xaml-markup-extensions#the-xstatic-markup-extension

    perfect - thanks!

    Tuesday, August 4, 2020 7:11 AM