简体   繁体   English

listview绑定到wpf类的对象

[英]listview bind to object of class wpf

I have the following AddedInfo class: 我有以下AddedInfo类:

public class AddedInfo : ViewModelBase
{
    private string m_infoString = "ppp";
    private ToggleButton m_infoButton = new ToggleButton();

    #region Properties

    public string InfoString
    {
        get { return m_infoString; }
        set
        {
            m_infoString = value;
            OnPropertyChanged();
        }
    }
    public ToggleButton InfoButton
    {
        get { return m_infoButton; }
        set
        {
            m_infoButton = value;
            OnPropertyChanged();
        }
    }

    #endregion Properties
}

In my ViewModel , I have the following definition: 在我的ViewModel ,我具有以下定义:

private ObservableCollection<AddedInfo> m_informationList = new ObservableCollection<AddedInfo>();

public ObservableCollection<AddedInfo> InformationList
{
    get { return m_informationList; }
    set
    {
        m_informationList = value;
        OnPropertyChanged();
    }
}

And in the Xaml : Xaml

<ListView Grid.Row="1" Background="Black" ItemsSource="{Binding InformationList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                      HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"/>

I would like the ListView to show only the ToggleButtons from class AddedInfo (meaning the InfoButton object). 我希望ListView仅显示类AddedInfo (表示InfoButton对象)中的ToggleButtons

Can such thing be done? 这样的事情可以做吗?

You should use ItemTemplate for the ListView. 您应该对ListView使用ItemTemplate You did not provide details about the ToggleButton class, but it should not be a control. 您没有提供有关ToggleButton类的详细信息,但它不应是控件。 In MVVM the viewmodels and models should contain only data. 在MVVM中,视图模型和模型应仅包含数据。 The control can be rendered in the view using DataTemplates (as ItemTemplate contains a DataTemplate). 可以使用DataTemplates在视图中呈现控件(因为ItemTemplate包含DataTemplate)。

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

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