0

I'm trying to bind the StringFormat of a column binding depending on each datacontext item's individually.

Here's the sample code:

xaml:

<ListView ItemsSource="{Binding Symbols}">
    <ListView.View>
        <GridView x:Name="gw">
            <GridView.Columns>
                <GridViewColumn Header="Symbol" DisplayMemberBinding="{Binding Name}"/>
                <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price, StringFormat={Binding StringFormat}}" />
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

code behind:

public ObservableCollection<symbol> Symbols { get;set;}

public class symbol : INotifyPropertyChanged    
    {

        #region INotify Handler

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
            => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

        #endregion


        private string _name;
        public string Name
        {
            get => _name;
            set
            {
                _name = value;
                OnPropertyChanged(nameof(Name));
            }
        }


        private double _price;
        public double Price
        {
            get => _price;
            set
            {
                _price = value;
                OnPropertyChanged(nameof(Price));
            }
        }


        private int _decimalplaces;
        public int decimalplaces
        {
            get => _decimalplaces;
            set
            {
                _decimalplaces = value;
                OnPropertyChanged(nameof(decimalplaces));
                if (value == 0)
                    StringFormat = "0";//no decimal 
                else
                    StringFormat = $"0.{new string('0', value)}";//like 0.000 for 3 decimal places
            }
        }

        public string StringFormat { get; set; }


    }

The StringFormat={Binding StringFormat} is not possible, I've just put it there to demonstrate what I exactly wanted. Each item's (symbol) format is different.

It doesn't matter if I need to add the columns in code behind, I can do it but I just don't know how to.

Any suggestions? Thank you.

1
  • One option would be to use a multibinding like was done in this answer. Commented Jan 18, 2023 at 1:18

1 Answer 1

0

Update: There is a solution I didn't want to use but now I believe it's the only logical way.

private double _price;
public double Price
{
    get => _price;
    set
    {
        _price = value;
        OnPropertyChanged(nameof(Price));
        StringPrice = value.ToString(format);
    }
}
private string _stringprice;
public string StringPrice
{
    get => _stringprice;
    set {
        _stringprice = value;
        OnPropertyChanged(nameof(StringPrice));
    }
}

and using it in XAML:

<GridViewColumn Header="Price" DisplayMemberBinding="{Binding StringPrice}" />
Sign up to request clarification or add additional context in comments.

1 Comment

Isn't that grid view read only? StringPrice could just have a lambda getter which formats Price. And Price raise property changes for StringPrice if necessary.

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.