C# Datagrid上的弹出控件
我这里有两个问题。我有一个数据网格,其中填充了一些项目。我希望在DataGrid选定行下显示一个弹出控件。以下是我所拥有的:C# Datagrid上的弹出控件,c#,wpf,mvvm,datagrid,popup,C#,Wpf,Mvvm,Datagrid,Popup,我这里有两个问题。我有一个数据网格,其中填充了一些项目。我希望在DataGrid选定行下显示一个弹出控件。以下是我所拥有的: <Grid> <DataGrid CanUserReorderColumns="False" CanUserSortColumns="False" HeadersVisibility="None" AutoGenerateColumns="False"
<Grid>
<DataGrid CanUserReorderColumns="False"
CanUserSortColumns="False"
HeadersVisibility="None"
AutoGenerateColumns="False"
VerticalAlignment="Stretch"
ItemsSource="{Binding ItemCollection}"
SelectedItem="{Binding SelectedItem}">
<DataGrid.Columns>
<DataGridTextColumn Width="*" Binding="{Binding Path=Key}" />
<DataGridTextColumn Width="*" Binding="{Binding Path=Value}" />
</DataGrid.Columns>
</DataGrid>
<Popup PopupAnimation="Scroll" Placement="Bottom" AllowsTransparency="True" IsOpen="{Binding PopupVisible}" Margin="0" StaysOpen="True" >
<local:PopupControl />
</Popup>
</Grid>
有了这段代码,我可以显示popover
第一个问题:使用StaysOpen=“True”移动窗口时,弹出窗口不会移动。我使用按钮控件处理这个问题的方法是将StaysOpen更改为“False”,这样当单击窗口中的其他位置时,弹出窗口将被取消。当我使用DataGrid控件执行此操作时,当SelectedItem被更改时,弹出窗口根本不会显示。为什么会这样
第二个问题:如何使弹出窗口显示在所选行下?我遇到了类似的问题,您是否使用Ajax作为弹出控件 这就是我使用ajax的方式 在正常代码下的页面顶部
<cc1:ModalPopupExtender ID="ModalPopupExtender_View" runat="server"
PopupControlID="popView" CancelControlID="cmdViewClose" TargetControlID="hidForModel" PopupDragHandleControlID="Panel1"
DropShadow="true" BackgroundCssClass="modalBackground" RepositionMode="None"></cc1:ModalPopupExtender>
隐藏字段用于设置弹出窗口在页面上的位置
单击您可以使用的按钮
ModalPopupExtender_View.Show();
在代码隐藏中,此解决方案对我有效: 第一期: 最初打开弹出窗口时,保存窗口坐标
Point coordinate = mainWindow.PointFromScreen(new Point(0,0));
xSavedWindowPos = coordinate.X;
ySavedWindowPos = coordinate.Y;
在主窗口的LocationChanged事件处理程序中:设置弹出窗口偏移量并保存新窗口位置
Point currentPos = (sender as Window).PointFromScreen(new Point(0,0));
yourPopup.HorizontalOffset += (xSavedWindowPos - currentPos.X);
yourPopup.VerticalOffset += (ySavedWindowPos - currentPos.Y);
xSavedWindowPos = currentPos.X;
ySavedWindowPos = currentPos.Y;
第二期:在datagrid的“CellMouseClick”事件处理程序中打开弹出窗口。您也可以设置StaysOpen。在弹出的xaml块中,更改datagrid的单元格单击事件中的Placement=“MousePoint”,然后更改IsOpen=“True”。datagrid控件中似乎没有CellMouseClick事件处理程序。是否有一些库或其他东西添加了此功能?另外,是否有任何方法可以使用MVVM实现这一点?WPF中根本没有DataGrid控件。所以,在WPF应用程序中,我使用了WinForms datagrid控件。这已经有了CellMouseClick事件。可能会有类似的活动。第一个问题如何?在DataGrid控件中用鼠标左键按下事件进行检查。如果您使用了popup(Placement=“MousePoint”)的“Placement”属性,则可以在该点上弹出。否则,您需要检查其他相关事件。第一个问题的解决方案确实有效。对于第二个问题,我能够使用MouseLeftButtonUp事件处理程序使其工作。问题是,我正在寻找解决这个问题的MVVM解决方案。你对我如何做到这一点有什么想法吗?所以我最终选择了另一条路线,在ListView上使用自定义ContextMenu来获取我想要的内容。你的解决方案确实回答了我的问题,所以我接受了。谢谢你的帮助。
Point currentPos = (sender as Window).PointFromScreen(new Point(0,0));
yourPopup.HorizontalOffset += (xSavedWindowPos - currentPos.X);
yourPopup.VerticalOffset += (ySavedWindowPos - currentPos.Y);
xSavedWindowPos = currentPos.X;
ySavedWindowPos = currentPos.Y;