将对象类型数据绑定到ListViewColumns

时间:2017-03-29 18:06:06

标签: c# wpf xaml data-binding

美好的一天,

我正在尝试将数据绑定到我的ListView列,但我遇到了麻烦。

public partial class ListViewItem
{
    public object Tag
    {
        get { return _tag; }
        set
        {
            if (value == _tag) return;
            _tag = value;
            OnPropertyChanged();
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

此标记属性设置为List<ActionObject>为简单起见,我将只在其中发布一个变量。

public class ActionObject : INotifyPropertyChanged
{
    private string _command;
    public string Command
    {
        get { return _command; }
        set
        {
            if (value == _command) return;
            _command = value;
            OnPropertyChanged();
        }
    }
}

这是XAML代码

<ListView x:Name="ActionLV" ItemsSource="{Binding ActionData.ListViewCollection}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Command" DisplayMemberBinding="{Binding Tag.Command, UpdateSourceTrigger=PropertyChanged}"/>
        </GridView>
    </ListView.View>
</ListView>

在下图中,您可以看到正在填充ListViewCollection,但它没有更新。

    private ObservableCollection<ListViewItem> _listViewCollection;
    public ObservableCollection<ListViewItem> ListViewCollection
    {
        get { return _listViewCollection; }
        set
        {
            if (value == _listViewCollection) return;
            _listViewCollection = value;
            OnPropertyChanged();
        }
    }

Proof that the collection is being populated

0 个答案:

没有答案