WPF Datagrid与单元格中的Viewbox鼠标右键选择(WPF Datagrid with a Viewbox in a cell block right mouse button selec

编程入门 行业动态 更新时间:2024-10-17 15:31:46
WPF Datagrid与单元格中的Viewbox鼠标右键选择(WPF Datagrid with a Viewbox in a cell block right mouse button selection)

我使用带有模板化单元格的数据网格来显示我的数据。 我可以添加我想到的任何类型的UI元素(DataTemplate,ContentControl,StackPanel,Rectangle),一切都还可以。 但是只要我添加一个ViewBox,右键单击鼠标按钮(在元素上)就会停止选择行/单元格。 左键单击继续正常工作。

据我所知,ViewBox应该只是可视化的,但不会影响任何事件路由。

重现问题的示例代码(红色矩形工作,绿色不工作):

<DataGrid ItemsSource="{Binding}" IsReadOnly="True" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" Margin="10"> <Rectangle Width="50" Height="50" Fill="Red" /> <Viewbox> <Rectangle Width="50" Height="50" Fill="Green" /> </Viewbox> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>

我尝试在子控件中捕获鼠标事件并将其发送给父级但没有成功。

I use a datagrid with templated cells to present my data. I can add any type of UI element I think of (DataTemplate, ContentControl, StackPanel, Rectangle) and everything still work fine. But as soon as I add a ViewBox, the right click mouse button (on the element) stop selecting the row/cell. Left click keep working properly.

As far as I know, a ViewBox is supposed to be visual only, but not to influence any event routing.

Sample code reproducing the problem (red rectangle works, green don't):

<DataGrid ItemsSource="{Binding}" IsReadOnly="True" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" Margin="10"> <Rectangle Width="50" Height="50" Fill="Red" /> <Viewbox> <Rectangle Width="50" Height="50" Fill="Green" /> </Viewbox> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>

I tried capturing the mouse event in the child control and send it to the parent but without success.

最满意答案

我在这个网站上找到了解决方案(Yves.Z的最后评论)

https://social.msdn.microsoft.com/Forums/vstudio/en-US/63fa1e10-1050-4448-a2bc-62dfe0836f25/selecting-datagrid-row-when-right-mouse-button-is-pressed?forum= WPF

I found a solution on this site (last comment by Yves.Z)

https://social.msdn.microsoft.com/Forums/vstudio/en-US/63fa1e10-1050-4448-a2bc-62dfe0836f25/selecting-datagrid-row-when-right-mouse-button-is-pressed?forum=wpf

更多推荐

本文发布于:2023-08-06 23:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1456656.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标右键   单元格   Viewbox   WPF   Datagrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!