wpf: How popup a usercontrol?

前端 未结 2 1785
日久生厌
日久生厌 2020-12-31 20:06

Background: I have a project which use the datagrid to display the data, and the datagrid has a rowdetail column which include a usercontrol. The usercontrol has some TextBo

相关标签:
2条回答
  • 2020-12-31 20:17

    You can use the Popup control. Attach a Popup to the current row of your datagrid, probably when you click the button is a good place to create the popup and place it as a child of one of cells in the row you are in.
    Then you can add the usercontrol to the popup and then 'show' the popup. This way, the DataContext for the popup and thus for the usercontrol is inherited from the containing row in the datagrid.

    Below is a very simple implementation of a Popup control just using XAML:

    <StackPanel>
      <CheckBox Name="chk1"/>
      <Popup IsOpen="{Binding IsChecked, ElementName=chk1}">
        <Border Background="White">
          <TextBlock Margin="20" Text="I'm Popped Up!"/>
        </Border>
      </Popup>
    </StackPanel>
    

    The popup is included in the stackpanel, but is only visible, IsOpen, when the checkbox is checked. You would place your usercontrol in the popup, where I have put the border and textblock. Since the popup is a member of the stackpanel, it automatically uses the DataContext of the stackpanel if it does not have one of its own.

    In your instance the stackpanel I am showing would be analogous to your DataGrid row.

    0 讨论(0)
  • 2020-12-31 20:33

    Well, I would like to just make a comment about Gate Lumas' comment but the system won't let me so I'm putting this down as an answer.

    XAML Popup sample: archive of link | original link

    If you download that sample and look at the .xaml and .cs files for Senario1, you'll have a perfect example of how to use and implement a popup. Alternatively you can just browse through the code on the site, but I find being able to run and interact with the sample more helpful than just looking at the code.

    0 讨论(0)
提交回复
热议问题