在 telerik Datagrid 中使用多重绑定

发布于 2024-12-13 06:56:05 字数 1904 浏览 6 评论 0原文

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

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

                <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>

更新:

我添加了一个名为选择项的类。看看转换器得到了什么。

    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"  />

但这也为空。这是为什么?

How do I pass multiple parameters in command parameters.

here is what I am trying to do:
I want to send Is checked or not (I can do this by introducing a Boolean field to the object bound. But I dont want to do that) and I want to send the selected data object for that row.

                <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>

UPDATE:

I added a class called selection Item. To see what the converter is getting.

    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;
    }

The type of the second parameter is the checkbox itself if I use

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

Here I want the data item that is bound to that row(in this case Customer).
I even tried using

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

But this is also coming in as null. why is this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无远思近则忧 2024-12-20 06:56:05

通过传入转换器当前绑定源来尝试简单的绑定,然后转换为基础对象并基于 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];
}

Try out simple binding by passing in converter current binding source, then cast to underlying object and based on IsChecked return a value.

<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;
 }

Now in command handler you would be able access parameters:

public void OnLineItemSelection(object parameter)
{
    object[] parameters = (object[])parameter;
    bool isChecked = (double)parameters[0];
    var instance = (TYPENAME)parameters[1];
}
鹿港小镇 2024-12-20 06:56:05

使用

<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>

就成功了

using

<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>

did the trick

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文