【问题标题】:Popup control on DatagridDatagrid 上的弹出控件
【发布时间】:2013-05-09 00:09:05
【问题描述】:

我在这里遇到了两个问题。我有一个填充了一些项目的 DataGrid。我想要发生的是在 DataGrid 选定行下显示一个 Popup 控件。这是我所拥有的:

<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>

当 SelectedItem 像这样更改时,我在 ViewModel 中设置 IsOpen 属性:

PopupVisible = true;

使用此代码,我可以显示弹出框。

第一个问题:使用 StaysOpen = "True" 移动窗口时弹出窗口不会移动。我使用 Button 控件处理此问题的方法是将 StaysOpen 更改为“False”,以便在单击窗口中的其他位置时关闭弹出窗口。当我使用 DataGrid 控件执行此操作时,当 SelectedItem 更改时,弹出窗口根本不会显示。这是为什么呢?

第二个问题:如何让弹出窗口显示在所选行下方?

【问题讨论】:

  • 在弹出的 xaml 块中,更改 Placement="MousePoint",然后在 datagrid 的单元格单击事件中更改 IsOpen="True"。

标签: c# wpf mvvm datagrid popup


【解决方案1】:

这个解决方案对我有用:

第一期: 最初打开弹出窗口时,保存窗口坐标。

Point coordinate = mainWindow.PointFromScreen(new Point(0,0));
xSavedWindowPos = coordinate.X;
ySavedWindowPos = coordinate.Y;

在 mainWindow 的 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;

第二个问题:在数据网格的“CellMouseClick”事件处理程序中打开弹出窗口。您也可以设置 StaysOpen。​​

【讨论】:

  • DataGrid 控件中似乎没有 CellMouseClick 事件处理程序。有没有一些图书馆或其他东西可以添加这个?另外,有没有办法使用 MVVM 来做到这一点?
  • DataGrid 控件最初在 WPF 中不存在。因此,在 WPF 应用程序中,我使用了 WinForms 数据网格控件。这有 CellMouseClick 事件。可能会有类似的活动。你的第一期怎么样?
  • 使用 DataGrid 控件中的鼠标左键按下事件进行检查。如果您完成了弹出窗口的“放置”属性 (Placement="MousePoint"),您可以在该点弹出。否则您需要检查其他相关事件。
  • 您对第一个问题的解决方案确实有效。对于第二个问题,我能够使用 MouseLeftButtonUp 事件处理程序使其工作。问题是我正在寻找一个 MVVM 解决方案。您对我如何做到这一点有任何想法吗?
  • 所以我最终选择了一条不同的路线,并在 ListView 上使用了自定义 ContextMenu 来获得我想要的东西。您的解决方案确实回答了我的问题,所以我接受了。感谢您的帮助。
【解决方案2】:

我也遇到过类似的问题,你是用 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(); 在后面的代码中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多