Silverlight中使用DataGrid后,里面的Button的Command不响应的问题
2012-02-16 21:44
429 查看
目前这个问题只针对Silverlight得到了解决。原因很简单,因为DataGrid一般在使用的时候都设置了ItemSource,这样里面的Command当然只会响应ItemSource里面的Command方法。这样一来,就需要在页面载入的时候,把页面的ViewModel保存下来,这样就暂时叫DataContext的代理吧,在使用的时候,调这个代理中的Command就可以了。代理可以单独地写成一个类。写法如下:
使用的时候需要在Xaml中添加对这个类所在的命名空间进行引用,例如:
然后在Xaml中实例化这个代理类,例如:
这样在DataGrid中的Button就可以响应命令了。使用如下
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Data; namespace Common { public class DataContextProxy : FrameworkElement { public DataContextProxy() { this.Loaded += (s, e) => { Binding binding = new Binding(); if (!string.IsNullOrEmpty(BindingPropertyName)) { binding.Path = new PropertyPath(BindingPropertyName); } binding.Source = this.DataContext; binding.Mode = BindingMode; this.SetBinding(DataContextProxy.DataSourceProperty, binding); }; } #region 依赖项属性 public Object DataSource { get { return (Object)GetValue(DataSourceProperty); } set { SetValue(DataSourceProperty, value); } } public static readonly DependencyProperty DataSourceProperty = DependencyProperty.Register("DataSource", typeof(Object), typeof(DataContextProxy), null); public string BindingPropertyName { get; set; } public BindingMode BindingMode { get; set; } #endregion } }
使用的时候需要在Xaml中添加对这个类所在的命名空间进行引用,例如:
xmlns:common="clr-namespace:GoldStock.Common;assembly=GoldStock.Common"
然后在Xaml中实例化这个代理类,例如:
<UserControl> <UserControl.Resources> <common:DataContextProxy x:Key="DataContextProxy"/> </UserControl.Resources> </UserControl>
这样在DataGrid中的Button就可以响应命令了。使用如下
<Button Content="修改" Command="{Binding Source={StaticResource DataContextProxy}, Path=DataSource.CommandModify}"/>
相关文章推荐
- WPF中使用DataGrid后,里面的Button的Command不响应的问题
- SilverLight学习笔记--如何解决Button不响应MouseLeftButtonDown与MouseLeftButtonUp事件的问题
- 关于使用Silverlight Toolkit 3 中的主题后DataGrid无法呈现数据的问题
- Silverlight中使用MVVM(7):DataGrid中触发Button的Click事件
- 在ListView的item项里使用CheckBox或者Button时,OnItemClickListener无响应的问题
- Silverlight中使用MVVM:DataGrid中触发Button的Click事件
- 在ListView的item项里使用CheckBox或者Button时,OnItemClickListener无响应的问题
- Silverlight中DataGrid翻页或者滚动时CheckBox/RadioButton显示的问题
- Silverlight中DataGrid翻页或者滚动时CheckBox/RadioButton显示的问题
- 利用xib自定义cell xib里面包含两个cell时点击Button没有响应问题
- 在ListView的item项里使用CheckBox或者Button时,OnItemClickListener无响应的问题
- Silverlight中使用MVVM(7):DataGrid中触发Button的Click事件
- Silverlight教程第三部分:使用 Networking取回数据并填充DataGrid
- 使用radion button选择datagrid的行(在datagrid中使用radio buttons)
- Silverlight中使用SDK的DataGrid,出现滚动条,每次滚动会触发LoadingRow事件
- JQuery UI 动态生成Dialog里面标签的事件重复打开不响应的问题
- 解决SilverLight项目使用DevExpress GridControl控件,修改列背景问题
- 在ASP.NET下使用jQuery UI下的Dialog,服务器端按钮无响应的问题
- ASP.NET控件使用——Button中关于CommandName 和 CommandArgument 的理解?????
- ECLIPSE使用appcompat_v7库无法找到android:Widget.Material.ActionButton的问题