【问题标题】:Using Multi Binding in telerik Datagrid在 Telerik Datagrid 中使用多重绑定
【发布时间】:2011-11-03 15:05:26
【问题描述】:

如何在命令参数中传递多个参数。

这是我想要做的: 我想发送是否选中(我可以通过将布尔字段引入对象绑定来做到这一点。但我不想这样做)并且我想发送该行的选定数据对象。

                <telerik:GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:GridViewDataControl}}, Path=DataContext.LineItemSelection}">
                            <CheckBox.CommandParameter>
                                <MultiBinding Converter="{StaticResource CommandConverter}">
                                    <Binding Path="IsChecked" />
                                    <Binding RelativeSource="{RelativeSource Self}"/>
                                </MultiBinding>
                            </CheckBox.CommandParameter>
                        </CheckBox>
                    </DataTemplate>
                </telerik:GridViewColumn.CellTemplate>

更新:

我添加了一个名为 selection Item 的类。看看转换器得到了什么。

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        SelectionItem si = new SelectionItem();
        foreach (var value in values)
        {
            Type t = value.GetType();
            if (t.FullName == "System.Boolean")
                si.IsSelected = (bool) value;
            else
            {
                si.SelectedCustomer = value as Customer;
            }
        }
        return si;
    }

如果我使用第二个参数的类型是复选框本身

    <Binding RelativeSource="{RelativeSource Self}"/>

这里我想要绑定到该行的数据项(在本例中为客户)。 我什至尝试使用

    <Binding RelativeSource= "{RelativeSource FindAncestor, AncestorType={x:Type telerik:GridViewColumn}}" Path="DataContext"  />

但这也以 null 的形式出现。这是为什么?

【问题讨论】:

    标签: wpf data-binding telerik mvvm-light telerik-grid


    【解决方案1】:

    通过传入转换器当前绑定源尝试简单绑定,然后转换为底层对象并基于 IsChecked 返回一个值。

    <DataTemplate>
          <CheckBox Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:GridViewDataControl}}, Path=DataContext.LineItemSelection}">
             <CheckBox.CommandParameter>
                   <MultiBinding Converter="{StaticResource CommandConverter}">
                         <Binding Path="IsChecked" />
                         <Binding RelativeSource="{RelativeSource Self}"/>
                    </MultiBinding>
             </CheckBox.CommandParameter>
           </CheckBox>
    </DataTemplate>
    
     // CommandConverter should simply return the values
     public object Convert(...)
     {
         return values;
     }
    

    现在在命令处理程序中,您将能够访问参数:

    public void OnLineItemSelection(object parameter)
    {
        object[] parameters = (object[])parameter;
        bool isChecked = (double)parameters[0];
        var instance = (TYPENAME)parameters[1];
    }
    

    【讨论】:

    • 我做了这个并调试了它。两个参数都为空。
    • 我添加了一个名为 selection Item 的类。看看转换器得到了什么。
    • 试试 uisng &lt;Binding Path="."/&gt; 而不是` `
    • 或者干脆&lt;Binding /&gt;希望对你有帮助
    【解决方案2】:

    使用

    <CheckBox Name="chkSelection" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:GridViewDataControl}}, Path=DataContext.LineItemSelection}">
        <CheckBox.CommandParameter>
            <MultiBinding Converter="{StaticResource CommandConverter}">
                <Binding Path="IsChecked" ElementName="chkSelection" />
                <Binding />
            </MultiBinding>
        </CheckBox.CommandParameter>
    </CheckBox>
    

    成功了

    【讨论】:

    • 我尝试将此添加到它自己的问题中。但无法看到代码(可能是我这边的格式错误)。这就是我在这里添加答案的原因。
    猜你喜欢
    • 2018-08-31
    • 2012-04-07
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    相关资源
    最近更新 更多