DataGridTemplateColumn 内的命令绑定

发布于 2024-11-18 07:38:30 字数 879 浏览 3 评论 0原文

我正在开发一个 Silverlight 应用程序,该应用程序广泛使用 Prism、MVVM 模式和 MEF。出于多种原因,我选择遵循视图优先的方法。

在其中一个视图中有一个 DataGrid,该网格的列之一是 DataGridTemplateColumn,其中只有一个按钮。

我想在按钮上定义命令和命令参数。 Command 应该是 ViewModel 的 DelegateCommand。 CommandParameter 应该是直接来自 dataGrid 的 SelectedItems 列表。

我尝试了多种方法来执行此操作,但 Command 或 CommandParameter 均为空。

它遵循我最初编写的代码:

<sdk:DataGridTemplateColumn>
    <sdk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Width="15" Height="15" Content=">" 
                    Command="{Binding UpdateSearchParametersCommand}" 
                    CommandParameter="{Binding SelectedItems, ElementName=dataGrid}">
        </DataTemplate>
    </sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>

有人可以建议我最好的方法是什么吗?

提前致谢, 吉安卢卡.

I am working on a Silverlight application which makes an extensive use of Prism, the MVVM pattern and MEF. For several reasons, I have chosen to follow a View-first approach.

In one of the Views there is a DataGrid, and one of the columns of this grid is a DataGridTemplateColumn, which has just a Button.

I'd like to define both a Command and a CommandParameter on the Button. The Command should be a DelegateCommand of the ViewModel. CommandParameter should be the SelectedItems list coming straight out of the dataGrid.

I've tried several approaches to do this, but either Command or CommandParameter are null.

It follows the code that I originally wrote:

<sdk:DataGridTemplateColumn>
    <sdk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Width="15" Height="15" Content=">" 
                    Command="{Binding UpdateSearchParametersCommand}" 
                    CommandParameter="{Binding SelectedItems, ElementName=dataGrid}">
        </DataTemplate>
    </sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>

Could someone advice me on what the best way to go about it is?

Thanks in advance,
Gianluca.

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

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

发布评论

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

评论(4

你们中的许多人都试图帮助我解决这个问题。谢谢你。
不幸的是,提供的答案大多与 WPF 有关。

这是我解决问题的方法:

<helpers:BindingHelper.Binding>
<helpers:BindingList>
     <helpers:RelativeSourceBinding TargetProperty="Command" Path="DataContext.ToggleDataArchiveInheritanceCommand" RelativeMode="FindAncestor" AncestorType="ChildWindow" />
</helpers:BindingList>
</helpers:BindingHelper.Binding>

好的,这来自同一应用程序的另一个点,但原理是相同的。
如果在 a 内部定义了绑定,则在 Silverlight 中访问通常超出范围的其他元素(因为它们不是 DataTemplate 的一部分)的唯一方法是遍历 xaml 对象树。这就是 BindingHelper 的作用。

在这里发帖是因为我希望这些信息对其他人有用。

干杯,

吉安卢卡

Many of you tried to help me out on this. Thank you for that.
Unfortunately the provided answers were mostly relative to WPF.

Here is how I've solved the problem:

<helpers:BindingHelper.Binding>
<helpers:BindingList>
     <helpers:RelativeSourceBinding TargetProperty="Command" Path="DataContext.ToggleDataArchiveInheritanceCommand" RelativeMode="FindAncestor" AncestorType="ChildWindow" />
</helpers:BindingList>
</helpers:BindingHelper.Binding>

Ok, this comes from another point of the same application, but the principle is the same.
If a binding is defined inside a , the only way you have in Silverlight to reach out other elements that normally would be out-of-scope (as they are not part of the DataTemplate) is to walk through the xaml object tree. That's what the BindingHelper does.

Posting here as I hope the information will be useful to someone else.

Cheers,

Gianluca

好菇凉咱不稀罕他 2024-11-25 07:38:30

您当前的绑定指向 DataGridRowItem.UpdateSearchParametersCommand。您需要将其更改为指向 DataGrid.DataContext.UpdateSearchParametersCommand

<sdk:DataGrid x:Name=dataGrid>
    <sdk:DataGridTemplateColumn>
        <sdk:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Width="15" Height="15" Content=">" 
                        Command="{Binding DataContext.UpdateSearchParametersCommand, ElementName=dataGrid}" 
                        CommandParameter="{Binding SelectedItems, ElementName=dataGrid}">
            </DataTemplate>
        </sdk:DataGridTemplateColumn.CellTemplate>
    </sdk:DataGridTemplateColumn>
</sdk:DataGrid>

Your current binding is pointing to DataGridRowItem.UpdateSearchParametersCommand. You need to change it to point to DataGrid.DataContext.UpdateSearchParametersCommand

<sdk:DataGrid x:Name=dataGrid>
    <sdk:DataGridTemplateColumn>
        <sdk:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Width="15" Height="15" Content=">" 
                        Command="{Binding DataContext.UpdateSearchParametersCommand, ElementName=dataGrid}" 
                        CommandParameter="{Binding SelectedItems, ElementName=dataGrid}">
            </DataTemplate>
        </sdk:DataGridTemplateColumn.CellTemplate>
    </sdk:DataGridTemplateColumn>
</sdk:DataGrid>
风尘浪孓 2024-11-25 07:38:30

如果您使用 ItemsSource 绑定 DataGrid,则 Command 和 CommandParameter 绑定将与当前项关联 - 按照您编写的方式。

在这种情况下,您应该使用替代来源。命令应绑定到 DataContext.UpdateSearchParametersCommand 和 CommandParameter - 绑定到 DataContext.SelectedItems。

在您的情况下,在绑定项中找不到 UpdateSearchParametersCommand 和 SelectedItems。

已更新

请务必为祖先设置正确的类型。我已将其设置为窗口,但也许您正在使用用户控件。

<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Button Width="15" Height="15" Content=">" 
                Command="{Binding Path=DataContext.UpdateSearchParametersCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" 
                CommandParameter="{Binding Path=DataContext.SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
    </DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>

If you bind your DataGrid using ItemsSource, then Command and CommandParameter binding is associated to the current item - the way you've written.

You should use alternative source in this case. Command should be binded to the DataContext.UpdateSearchParametersCommand and CommandParameter - to DataContext.SelectedItems.

In your case neither UpdateSearchParametersCommand, nor SelectedItems cannot be found in the binded item.

UPDATED

Be sure to set the right type for ancestor. I've set it to window, but maybe you are using UserControl.

<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Button Width="15" Height="15" Content=">" 
                Command="{Binding Path=DataContext.UpdateSearchParametersCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" 
                CommandParameter="{Binding Path=DataContext.SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
    </DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>

戏剧牡丹亭 2024-11-25 07:38:30

在 silverlight 5 中,您可以执行此操作,

<Button Command="{Binding Path=DataContext.PreviewPublishCommand, RelativeSource={RelativeSource AncestorType=controls:ChildWindow}}" Content="Publish" />

只需将 AncestorType 调整为您的顶级元素(UserControl、ChildWindow 等)。

In silverlight 5 you can do this

<Button Command="{Binding Path=DataContext.PreviewPublishCommand, RelativeSource={RelativeSource AncestorType=controls:ChildWindow}}" Content="Publish" />

Just adjust AncestorType to be whatever your top level element is (UserControl, ChildWindow, etc).

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