我使用带有模板化单元格的数据网格来显示我的数据。 我可以添加我想到的任何类型的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
更多推荐
发布评论