与“保护方法”不一致的行为在数据网格内

发布于 2024-12-16 00:31:13 字数 1236 浏览 4 评论 0原文

我有一个与此类似的 DataGrid

<sdk:DataGrid ItemSource="ItemGroups">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTemplateColumn>
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Button 
                            Content="Show Items" 
                            cal:Message.Attach="ShowItems($dataContext)" />
                    </StackPanel>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>
        <sdk:DataGridTextColumn Binding="{Binding Name}" Header="Name" />
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

在我的视图模型中,我有一个防护方法:

public bool CanShowItems(ItemGroup itemGroup)
    {
        return itemGroup.State == States.Active;
    }

在我当前的场景中,防护方法仅针对列表中的第二项返回 true

所有其他“ShowItems”按钮均应被禁用。

但是,当我向下滚动 DataGrid 时,启用的按钮消失,另一个启用的按钮进入视图。不会同时显示两个启用的按钮。但是,同样,只有一次对 Guard 方法的调用返回 true

是什么导致了这种奇怪的行为?

任何帮助将不胜感激。

I have a DataGrid similar to this one:

<sdk:DataGrid ItemSource="ItemGroups">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTemplateColumn>
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Button 
                            Content="Show Items" 
                            cal:Message.Attach="ShowItems($dataContext)" />
                    </StackPanel>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>
        <sdk:DataGridTextColumn Binding="{Binding Name}" Header="Name" />
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

In my view model I have a guard method:

public bool CanShowItems(ItemGroup itemGroup)
    {
        return itemGroup.State == States.Active;
    }

In my current scenario the guard method returns true for only the second item in the list.

All the other "ShowItems" button are disabled as they should be.

However, when I scroll down the DataGrid as the enabled button goes out of view another enabled button comes into view. There won't be two enabled buttons visible at the same time. But, again, there is only one call to the guard method that returns true.

What could be causing this odd behavior?

Any help would be greatly appreciated.

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

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

发布评论

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

评论(1

空城之時有危險 2024-12-23 00:31:13

这可能是由于 DataGrid 和类似控件回收 DataTemplate 造成的。这会导致一些与您所描述的类似的问题。一般来说,我会避免将操作直接放在行上。这可能会导致创建大量行为,尤其是在您拥有大量数据的情况下。您可能需要完全不同的方法。

This is likely caused by the fact that the DataGrid and similar controls recycle DataTemplates. This causes some issues similar to what you are describing. In general, I would avoid putting actions directly on rows. That can result in a lot of behaviors being created, especially if you have a lot of data. You might need to a different approach altogether.

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