简体   繁体   English

WPF GridView gridviewcolumn 绑定上每个项目的不同字符串格式

[英]WPF GridView different stringformat for each item on gridviewcolumn binding

I'm trying to bind the StringFormat of a column binding depending on each datacontext item's individually.我正在尝试根据每个数据上下文项目分别绑定列绑定的 StringFormat。

Here's the sample code:这是示例代码:

xaml: 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. StringFormat={Binding StringFormat}是不可能的,我只是把它放在那里来演示我到底想要什么。 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.谢谢你。

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:并在 XAML 中使用它:

<GridViewColumn Header="Price" DisplayMemberBinding="{Binding StringPrice}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM