我有以下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
对象)。
可以这样做吗?
答案 0 :(得分:1)
您应该使用ItemTemplate作为ListView。您没有提供有关ToggleButton类的详细信息,但它不应该是控件。在MVVM中,视图模型和模型应仅包含数据。可以使用DataTemplates在视图中呈现控件(因为ItemTemplate包含DataTemplate)。