我有一个包含DataGrid的usercontrol(datagrid允许用户在网格上编辑),但是当我通过viewmodel属性从datagrid获取集合数据时,但集合只获取行数而不是获取datagrid内容。
查看
<UserControl>
<Grid>
<DataGrid ItemsSource="{Binding}" SelectionUnit="CellOrRowHeader" CanUserAddRows="True" CanUserDeleteRows="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Day" Binding="{Binding Monday}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
视图模型
public class RuleDetailViewModel : UserControlViewModelBase
{
ObservableCollection<RuleDetailExpViewModel> _ruleDetailList;
public RuleDetailViewModel()
{
_ruleDetailList = new ObservableCollection<RuleDetailExpViewModel>
{
new RuleDetailExpViewModel(),
new RuleDetailExpViewModel()
};
}
public ObservableCollection<RuleDetailExpViewModel> RuleDetailList
{
get { return _ruleDetailList; }
set
{
_ruleDetailList = ruleDetailList;
OnPropertyChanged("RuleDetailList");
}
}
}
我尝试了ItemsSource =“{Binding RuleDetailList}”和ItemsSource =“{Binding}”DataContext =“{Binding RuleDetailList}”但是不成功。
如何从view到viewmodel获取数据?
抱歉我的英文
答案 0 :(得分:1)
将您的观点更改为
<UserControl>
<Grid>
<DataGrid ItemsSource="{Binding RuleDetailList}" SelectionUnit="CellOrRowHeader" CanUserAddRows="True" CanUserDeleteRows="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Day" Binding="{Binding Monday}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
且RuleDetailExpViewModel
必须包含属性Monday