与“保护方法”不一致的行为在数据网格内
我有一个与此类似的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是由于
DataGrid
和类似控件回收DataTemplate
造成的。这会导致一些与您所描述的类似的问题。一般来说,我会避免将操作直接放在行上。这可能会导致创建大量行为,尤其是在您拥有大量数据的情况下。您可能需要完全不同的方法。This is likely caused by the fact that the
DataGrid
and similar controls recycleDataTemplate
s. 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.