DataGridTemplateColumn 内的绑定命令

发布于 2024-12-07 02:28:32 字数 4291 浏览 0 评论 0原文

我使用命令将视图(包括 XAML)附加到我的 ViewModel。当单击 DataGrid 行上的按钮时,我需要调用命令。我正在为此使用行为(常规命令也有同样的问题)。当我单击 DataGrid 上的按钮时 - 我的命令不会被触发。

为了说明问题 - 我将 ListBox 放在底部,并具有完全相同的绑定内容 - 是的,命令有效。所以,这与 DataGrid/DataGridTemplateColumn 有关

<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding}">
            <Grid.RowDefinitions>
                <RowDefinition Height="30" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal">
                <Button Content="Cancel" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <ei:CallMethodAction MethodName="Cancel" TargetObject="{Binding}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
            </StackPanel>
            <sdk:DataGrid AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding Data}" Grid.Row="1">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTemplateColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Select">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="Click">
                                            <i:InvokeCommandAction Command="{Binding ElementName=Control, Path=DataContext.ItemSelectedCommand}" CommandParameter="{Binding}" />
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </Button>  
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTextColumn Binding="{Binding DeviceId}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Device" Width="Auto" FontWeight="Bold" />
                    <sdk:DataGridTextColumn Binding="{Binding SerialNumber}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Serial Number" Width="Auto" />
                    <sdk:DataGridTextColumn Binding="{Binding LastActivityOn}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Last Activity" Width="Auto" />
                    <sdk:DataGridTextColumn Binding="{Binding ClientVersion}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Client Version" Width="Auto" />
                    <sdk:DataGridTextColumn Binding="{Binding OSVersion}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="OS Version" Width="Auto" />
                </sdk:DataGrid.Columns>

            </sdk:DataGrid>
            <ListBox Grid.Row="2" ItemsSource="{Binding Data}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding DeviceId}"></TextBlock>
                            <Button Content="Select">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <i:InvokeCommandAction Command="{Binding ElementName=Control, Path=DataContext.ItemSelectedCommand}" CommandParameter="{Binding}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Button>
                        </StackPanel>                        
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>

I have View(XAML included) attached to my ViewModel with commands. I need to invoke command when Button on DataGrid's row clicked. I'm using behaviors for this (regular commanding have same issue). When I click button on DataGrid - my command does not get fired.

To illustrate issue - I placed ListBox on a bottom with EXACT same binding stuff - and yes, command works. So, it's something with DataGrid/DataGridTemplateColumn

<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding}">
            <Grid.RowDefinitions>
                <RowDefinition Height="30" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal">
                <Button Content="Cancel" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <ei:CallMethodAction MethodName="Cancel" TargetObject="{Binding}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
            </StackPanel>
            <sdk:DataGrid AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding Data}" Grid.Row="1">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTemplateColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Select">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="Click">
                                            <i:InvokeCommandAction Command="{Binding ElementName=Control, Path=DataContext.ItemSelectedCommand}" CommandParameter="{Binding}" />
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </Button>  
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTextColumn Binding="{Binding DeviceId}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Device" Width="Auto" FontWeight="Bold" />
                    <sdk:DataGridTextColumn Binding="{Binding SerialNumber}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Serial Number" Width="Auto" />
                    <sdk:DataGridTextColumn Binding="{Binding LastActivityOn}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Last Activity" Width="Auto" />
                    <sdk:DataGridTextColumn Binding="{Binding ClientVersion}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Client Version" Width="Auto" />
                    <sdk:DataGridTextColumn Binding="{Binding OSVersion}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="OS Version" Width="Auto" />
                </sdk:DataGrid.Columns>

            </sdk:DataGrid>
            <ListBox Grid.Row="2" ItemsSource="{Binding Data}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding DeviceId}"></TextBlock>
                            <Button Content="Select">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <i:InvokeCommandAction Command="{Binding ElementName=Control, Path=DataContext.ItemSelectedCommand}" CommandParameter="{Binding}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Button>
                        </StackPanel>                        
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>

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

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

发布评论

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

评论(2

指尖微凉心微凉 2024-12-14 02:28:32

我很确定在 DataGrid 中,您仍然需要 DataContextProxy 使绑定正常工作。 ElementName 绑定不起作用(是的,它适用于 ListBox 模板,但不适用于 DataGrid,这是因为 DataGridTemplateColumn 不在可视化树中),即使在 Silverlight 中也是如此4.

更新

在 Silverlight 5 中,由于对 Ancestor 绑定的支持,DataGrid 中不再需要 DataContextProxy

示例

<Button Command="{Binding DataContext.CancelCommand, RelativeSource={RelativeSource AncestorType=sdk:DataGrid}}" />

I am pretty sure inside a DataGrid, you still need the DataContextProxy to make bindings working. The ElementName binding doesn't work (yes, it is working for ListBox templates, but not DataGrid, this is because DataGridTemplateColumn is not in the visual tree), even in Silverlight 4.

UPDATE

In Silverlight 5, DataContextProxy is not needed in a DataGrid thanks to the support of Ancestor binding.

Example

<Button Command="{Binding DataContext.CancelCommand, RelativeSource={RelativeSource AncestorType=sdk:DataGrid}}" />
思慕 2024-12-14 02:28:32

我认为问题在于这一行

<i:InvokeCommandAction Command="{Binding ElementName=Control, Path=DataContext.ItemSelectedCommand}" CommandParameter="{Binding}" />

,因为您的 XAML 中没有使用名称 Control (又名。x:Name =“Control”)定义的元素。

如果该命令是在绑定到 LayoutRoot 的 DataContext 上定义的,则应将 ElementName=Control 更改为 ElementName=LayoutRoot

我不知道为什么这在列表框中有效。

I think the problem is this line

<i:InvokeCommandAction Command="{Binding ElementName=Control, Path=DataContext.ItemSelectedCommand}" CommandParameter="{Binding}" />

cause in your XAML is no element defined with name Control (aka. x:Name="Control").

You should change ElementName=Control to ElementName=LayoutRoot if the command is defined on the DataContext that is bound to LayoutRoot.

I don´t know why this works in the ListBox.

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