wpf mvvm datagrid DataGridTemplateColumn的绑定无效的可能原因之一!
昨天在mvvm wpf的开发中遇到一个问题,绑定不起作用,编辑阶段没问题也没有提示找不到对应的绑定,但是在运行之后却不起作用,查了很多资料,说法不一,有些是要删除datagrid的一行,直接绑定delete即可,但是我要绑定的是viewmodel的command,最终解决问题了,特分享如下(已解决):
view:
<DataGrid ItemsSource="{Binding TodoList}"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
CanUserSortColumns="False"
IsReadOnly="True"
SelectedItem="{Binding SelectedItem}">
<DataGrid.Columns>
<DataGridTextColumn Header="Title"
Binding="{Binding Title}">
</DataGridTextColumn>
<DataGridTextColumn Header="Text"
Binding="{Binding Text}">
</DataGridTextColumn>
<DataGridTextColumn Header="InsertDateTime"
Binding="{Binding InsertDateTime}">
</DataGridTextColumn>
<DataGridTextColumn Header="EstimateDateTime"
Binding="{Binding EstimateDateTime}">
</DataGridTextColumn>
<DataGridTemplateColumn Header="Delete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete"
Command="{Binding DeleteCommand}">
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
其中
DataGridTemplateColumn的button的command绑定无效,不起作用,
viewmodel:
private ICommand _deleteCommand;
public ICommand DeleteCommand
{
get { return _deleteCommand; }
set
{
_deleteCommand = value;
RaisePropertyChanged();
}
}
public TodoViewModel()
{
FreshCommand = new RelayCommand(DoFreshCommand, () => true);
DeleteCommand = new RelayCommand(DoDeleteCommand, () => true);
DoneCommand = new RelayCommand(DoDoneCommand, () => true);
}
private void DoDeleteCommand()
{
if (SelectedItem == null) return;
MongoDbProvide.Delete<TodoInfos>(p => p._id == SelectedItem._id);
TodoList.Remove(SelectedItem);
}
修改view如下解决:
<DataGridTemplateColumn Header="Delete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete"
Command="{Binding ElementName=TodoViewTitle,
Path=DataContext.DeleteCommand}">
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
TodoViewTitle 为这个view的名称 总结:因datagrid已经指定了
ItemsSource="{Binding TodoList}"
所以内部的列的绑定会默认去找这个
ItemsSource的属性,如果要绑定一个独立的dp,必须要在绑定时指定element和path。
还有其他的列表控件listbox等也是一样,在编辑阶段时,通过view的绑定可以直接找到command,但是是不起作用的,要指定绑定的element和path.
wpf mvvm datagrid DataGridTemplateColumn的绑定无效的可能原因之一!的更多相关文章
- wpf mvvm datagrid 中button绑定命令方法
<DataGridTemplateColumn Header="设备状态" IsReadOnly="True" Width="150" ...
- wpf mvvm datagrid数据过滤
原文:wpf mvvm datagrid数据过滤 datagrid数据过滤,你可以通过设置RowStyle属性,通过将Visibility绑定到ViewModel层的属性来控制是否可见,比如: & ...
- (WPF) MVVM: DataGrid Binding
Binding到DataGrid的时候,需要用到ObservableCollection. public ObservableCollection<Customer> Customers ...
- WPF中DataGrid的应用-绑定,增改删,分页,样式
参考以下网址: http://www.cnblogs.com/fwbnet/archive/2012/05/08/2490974.html
- WPF MVVM 键盘按键事件绑定
<TextBox Text="{Binding LoginName}" Height="35" Canvas.Left="306" T ...
- WPF的DataGrid绑定ItemsSource后第一次加载数据有个别列移位的解决办法
最近用WPF的DataGrid的时候,发现一个很弱智的问题,DataGrid的ItemsSource是绑定了一个属性: 然后取数给这个集合赋值的时候,第一次赋值,就会出现列移位 起初还以为是显卡的问题 ...
- WPF中DataGrid的ComboBox的简单绑定方式(绝对简单)
在写次文前先不得不说下网上的其他wpf的DataGrid绑定ComboBox的方式,看了之后真是让人欲仙欲死. 首先告诉你一大堆的模型,一大堆的控件模板,其实或许你紧紧只想知道怎么让combobox怎 ...
- WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件)对象绑定
原文:WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件)对象绑定 WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件) 上面的 ...
- WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件)
原文:WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件) 因为项目需要 要实现这个~ 怎么实现画红框内容部分 田字格和上面Textbox 属于一个自定义控件 大 ...
随机推荐
- android源码、博文2
每周精选 第 54 期 精品源码 仿网易新闻app下拉标签选择菜单 仿网易新闻app下拉标签选择菜单,长按拖动排序,点击增删标签控件##示例 https://github.com/we ...
- Watson Product
This article will discuss Watson related products or services. I will add more detailed information ...
- LCS 算法实现
动态规划算法 #include <iostream> #include <string.h> #include <algorithm> #include <m ...
- js如何获取样式?
在某个项目中,我们经常会需要来获取某个元素的样式,比如说获取一个div的color:这样,新的问出现了, var style = box.style.width;console.log(style); ...
- 面向对象15.3String类-常见功能-获取-2
public class String_APImethod {/* * 1.4获取字符串中的一部分字符串,也叫字符串 * String substring(int beginIndex, int en ...
- 阿里巴巴2018届应届生在线编程测验-研发工程师C/C++
刚才去做了阿里巴巴的编程测验,好消息是抽到的题相对别的题简单一些,坏消息是编的太慢了,没有做完. 现在把题目和自己后来编出来的代码贴在这里,供大家参考. 题目: 1. 从命令行输入若干个正整数(大于等 ...
- 如何给Ionic写一个cordova插件
写一个cordova插件 之前由javaWeb转html5开发,由于面临新技术,遂在适应的过程中极为挣扎,不过还好~,这个过程也极为短暂:现如今面临一些较为复杂的需求还会有一丝丝头痛,却没有一开始那么 ...
- Android - 多语言自动适配
Android为多语言适配提供了很大的方便.开发者不需要在代码中进行修改.只需要配置xml文件. res --> values 其中存放有xml文件.一般这些都是英文的字符串.我们可以存放其他语 ...
- hdu_1711: Number Sequence【KMP算法】
题目链接 此次插播点笔记 hdu中点击蓝色的"Compilation Error"可以查看自己是为什么CE的 hdu中提交的话,语言选择G++可以使用<bits/stdc++ ...
- maven-编译速度优化
故障描述: 公司搭建了一个新jenkins持续集成环境,jenkins构建job时间越来越长. 原因分析: 系统CPU限制:判断依据,构建中查看日志 tail -f /var/log/messages ...