0

I have this XAML:

<Label Text="{Binding val1, StringFormat={StaticResource CommaInteger}}"/>
<Label Text="{Binding val2, StringFormat={StaticResource CommaInteger}}"/>

I want to simplify it to:

<local:commaIntLbl Text="{Binding val1}"/>
<local:commaIntLbl Text="{Binding val2}"/>

How should I code my commaIntLbl class to achieve this?

public class commaIntLbl : Label
{
    public commaIntLbl() : base()
    {
        SetDynamicResource(StyleProperty, "IntegerLabel");
        // How to refer its Text's string-format to the StaticResource CommaInteger?
    }
}

1 Answer 1

1

You could set the string format in val1 property like below and then do the binding.

 public string _val1;
    public string val1
    {
        get
        {
            return string.Format("Hello, {0}", _val1);
        }
        set
        {
            _val1 = value;
        }
    }

Update:

Xaml:

 <local:CommaIntLbl TextVal="{Binding val1}" />

Custom Control:

 public class CommaIntLbl : Label
{
    public CommaIntLbl() : base()
    { }
    private string _text;
    public string TextVal
    {
        get { return _text; }
        set
        {
            _text = value;
            OnPropertyChanged();
        }
    }
    public static readonly BindableProperty TextValProperty = BindableProperty.Create(
             nameof(TextVal),
             typeof(string),
             typeof(CommaIntLbl),
             string.Empty,
             propertyChanged: (bindable, oldValue, newValue) =>
             {
                 var control = bindable as CommaIntLbl;
                 control.Text = String.Format("Hello, {0}", newValue);
             });
}

Code behind:

public string _val1;
    public string val1 { get; set; }

    public Page19()
    {
        InitializeComponent();
        val1 = "1234";
        this.BindingContext = this;
    }
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer, but that doesn't actually solve my scenario. The binding context is not something under me. I just know that that particular field is a number, and I want to apply that string format to it. Also, there are actually tens of such labels in my XAML.
One is that you could change the string format in the proprty. Another is to change it through the converter.
Wendy, thanks for the suggestion. I don't know how a converter is supposed to work, and I've read a bit about it. It seems that the converter is applied to the outsider (val1 or val2), not to the class commaIntLbl itself. If that's the case, then it doesn't serve my purpose. As I've said, the binding context is a class not under me. I can't modify that class. If the converter can be coded inside commaIntLbl, perhaps you can show me a short snippet of how it is implemented? Thanks again!
For the Converter, it still need to do something with xaml. I would test the way inside commalntLbl.
@Dunnomuch You could use bindable property to do that. Please check my update.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.