OrMiz는보기에 대한보기 모델을 구현하는 것이 정확합니다.
저는 MVVM 전문가가 아니지만 여기에 어떻게 할 수 있는지 보여주는 예가 있습니다. 피드백 환영합니다.
그룹 테이블을 나타내는 클래스 ...
public class GroupTable
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
목록보기에서 하나 개의 행을 나타낼 수 뷰 모델 유리 ... 그런
public class GroupViewModel : INotifyPropertyChanged
{
public GroupViewModel(GroupTable group)
{
this.GroupName = group.Name;
this.GroupIsChecked = GroupCheckedLogic();
}
private string _GroupName;
public string GroupName
{
get { return _GroupName; }
set
{
_GroupName = value;
OnPropertyChanged();
}
}
private bool _GroupIsChecked;
public bool GroupIsChecked
{
get { return _GroupIsChecked; }
set
{
_GroupIsChecked = value;
OnPropertyChanged();
}
}
private bool GroupCheckedLogic()
{
// Your logic goes here
return true;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
메인 뷰 모델 보기 ... 당신의 XAML에서 그런
public class GroupListViewModel : INotifyPropertyChanged
{
public GroupListViewModel()
{
// Get your actual data
var groupsData = new List<GroupViewModel>
{
new GroupViewModel(new GroupTable {Id = 1, Name = "Group 1"}),
new GroupViewModel(new GroupTable {Id = 2, Name = "Group 2"})
};
this.Groups = new ObservableCollection<GroupViewModel>(groupsData);
}
private ObservableCollection<GroupViewModel> _Groups;
public ObservableCollection<GroupViewModel> Groups
{
get { return _Groups; }
set
{
_Groups = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
...
,
<ListView Name="UserConfigurationListView" ItemsSource="{Binding Groups}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding GroupName}" />
<CheckBox IsChecked="{Binding GroupIsChecked}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListView>
'그룹'은'그룹'개체의 테이블 또는 C# 모음입니까? MVVM 방식으로하고 싶습니다. 이 경우'Group' 클래스에'IsChecked' 속성을 가질 수 있습니다. – niksofteng