listview绑定到类wpf

时间:2016-11-23 14:06:26

标签: c# wpf listview binding togglebutton

我有以下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
}

在我的ViewModel中,我有以下定义:

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

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

Xaml

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

我希望ListView仅显示来自ToggleButtons类的AddedInfo(意为InfoButton对象)。

可以这样做吗?

1 个答案:

答案 0 :(得分:1)

您应该使用ItemTemplate作为ListView。您没有提供有关ToggleButton类的详细信息,但它不应该是控件。在MVVM中,视图模型和模型应仅包含数据。可以使用DataTemplates在视图中呈现控件(因为ItemTemplate包含DataTemplate)。