wpf 如何将参数通过CommandParameter 传入viewmodel

7 篇文章 0 订阅

  今天需要实现这么一个功能,在datagrid里,每行增加编辑按钮,弹出对话框修改签注。xmal代码如下:

<xcdg:Column FieldName="Button" Title=""  Width="70">
<xcdg:Column.CellContentTemplate>
   <DataTemplate>
       <StackPanel Orientation="Horizontal">
           <Button Margin="2,0,2,0" Height="18px" Content="{Binding Converter={StaticResource T}, ConverterParameter=Administration#StrMeaningsEdit}"
               Command="{Binding Path=DataContext.MeaningEditCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"
               CommandParameter="{Binding ElementName=G}"></Button>                                    
           <Button Margin="2,0,0,0" Content="{Binding Converter={StaticResource T}, ConverterParameter=Administration#StrMeaningsDelete}"
               Command="{Binding Path=DataContext.MeaningDeleteCmd, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"
               CommandParameter="{Binding Path=.}"></Button>                                    
       </StackPanel>
   </DataTemplate>
</xcdg:Column.CellContentTemplate>
</xcdg:Column>


变成下面这个图


这中间就需要把窗体grid作为参数传到view'model来。 这样 通过名称才能找到控件,控制窗体显示。

public ICommand MeaningEditCommand
{
    get
    {
        if (_MeaningEditCommand == null)
        {
            _MeaningEditCommand = new DelegateCommand<Grid>(G =>
            {
                TextBox TB = G.FindName("txtName") as TextBox; 
                TB.Text = CurrentMeanings.Desc;
                Canvas C = G.FindName("Operate") as Canvas;
                C.Visibility = C.Visibility == Visibility.Visible ? Visibility.Hidden : Visibility.Visible;
                MeaningOperate = 2;
            });
        }
        return _MeaningEditCommand;
    }
}

总结: 首先 如果需要edit delte响应事件,必须使用RelativeSource标签,才能使得事件正常响应,但是传的参数却需要全局的grid。这该如何是好。

感觉解决方案如下:

If you want to pass the Button as a parameter:

<Button Command="CommandGetsCalled" 
        CommandParameter="{Binding RelativeSource={RelativeSource Self}}" />

If you want to pass the Window as a parameter:

<Button Command="CommandGetsCalled" 
        CommandParameter="{Binding RelativeSource={
             RelativeSource AncestorType={x:Type Window}}}" />
使用第二种解决问题。


关键是对mvvm模式下,relativeSource的理解。

WPF绑定使用的源属性必须是依赖项属性,这是因为依赖项属性具有内置的更改通知支持,元素绑定表达式使用了Xaml扩展标记,WPF绑定一个控件是使用Binding.ElementName,绑定非控件对象时使用Source,RelativeSource,DataContext属性(WPF特有,而非XAML),只能绑定对象的共有字段.

① Source:数据提供者
② RelativeSource:根据当前对象为基础,自动查找源并绑定
③ DataContext:如果未使用Source和RelativeSource,WPF就从当前控件开始在控件树种向上查找,并使用第一个非空的DataContext属性,可以在更高层次容器对象上设置DataContext,如下代码 Text 绑定到 Source属性,但未设置Text的绑定对象,会向上查找DataContext绑定的对象的Source属性

PF中派生自ItemsControl的类都能显示列表,能够支持集合数据绑定的元素包括ListBox,ComboBox,ListView和DataGrid,Menu,Treeview,ItemsControl中有三个重要属性:

① ItemsSource: 指向一个集合,结合必须支持IEnumerable接口,该集合包含将在列表中显示的所有元素,但基本的IEnumerable接口只支持只读绑定,要使修改能直接反应到绑定的控件上需要使用ObservablCollection类
② DisplayMemberPath:确定用于显示的 对象的属性,如果未设置 则会显示对象的ToString()方法返回的值
③ ItemTemplates:接受一个数据模板,用于为每个项创建可视化外观

没有Path的Binding:Binding源本身就是数据且不需要Path来指明,如下绑定表示将Text绑定到字符串类型mystring,mystring本身就是数据.Path后为"."或者空表示绑定source本身:

<TextBlock Text="{Binding source={StaticResource ResourceKye=mystring},Path=.}">


参考地址:http://stackoverflow.com/questions/3504332/passing-the-current-window-as-a-commandparameter

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值