C# wpf:如何弹出用户控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18719977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:01:11  来源:igfitidea点击:

wpf: How popup a usercontrol?

c#wpfmvvmdatagriduser-controls

提问by Gate Luma

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 TextBox for user inputing and displaying some message.

背景:我有一个使用数据网格显示数据的项目,数据网格有一个包含用户控件的 rowdetail 列。用户控件有一些 TextBox 用于用户输入和显示一些消息。

Problem: I want to make the usercontrol popup when I click a button, and the popuped usercontrol has the same context as the usercontrol's in the rowdetail column of datagrid. The intention of doing so is to make it easy for users to interactive with the usercontrol because that room of the rowdetail cell is limited.

问题:我想在单击按钮时弹出 usercontrol,并且弹出的 usercontrol 与 datagrid 的 rowdetail 列中的 usercontrol 具有相同的上下文。这样做的目的是让用户更容易与用户控件交互,因为 rowdetail 单元格的空间是有限的。

The usecontrol has be implemente and it can work normally in the rowdetail cell. However, I have no idea about how to pop it up without change its context, such as data source, messages have been display in the TextBox, etc.. Anyone can give me some advices? By the way, I use the MVVM pattern.

usecontrol已经实现,可以在rowdetail单元中正常工作。但是,我不知道如何在不更改上下文的情况下弹出它,例如数据源,文本框中显示的消息等。有人可以给我一些建议吗?顺便说一下,我使用的是 MVVM 模式。

EDIT:Here is the RowDetailTemplate:

编辑:这是 RowDetailTemplate:

    <DataTemplate x:Key="RowDetailTemplate">
    <Grid x:Name="RowDetailGrid" HorizontalAlignment="Left" Width="850" Height="355" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="100" />
        </Grid.ColumnDefinitions>
        <my:MyUserControl x:Name="myUserControl" />
        <Button Grid.Row="1" Width="60" Height="25" Content="Pop Up" 
                    Command="{Binding Path=PopupCommand}"/>
        .....
    </Grid>
</DataTemplate>

Here is the usercontrol (MyUserControl in the above codes):

这是用户控件(上面代码中的 MyUserControl):

<UserControl x:Class="MyProject"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="600">
<Grid>
    <ScrollViewer  Margin="0" HorizontalScrollBarVisibility="Disabled" >
        <StackPanel>
            <ItemsControl Grid.Row="0" ItemsSource="{Binding Output, Mode=OneWay}" FontSize="12">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                            <TextBlock TextWrapping="Wrap" Text="{Binding Path=.}" Foreground="White" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
            <DockPanel Grid.Row="1" LastChildFill="True">                    
                <TextBox Text="{Binding Input, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                     TextWrapping="Wrap"
                     BorderBrush="{x:Null}" SelectionBrush="{x:Null}" 
                     BorderThickness="0" Width="Auto">
                    <TextBox.InputBindings>
                        <KeyBinding Command="{Binding Path=TextBoxEnter}" Key="Enter" />
                    </TextBox.InputBindings>
                </TextBox>
            </DockPanel>
        </StackPanel>
    </ScrollViewer>
</Grid>

采纳答案by Stewbob

You can use the Popupcontrol. Attach a Popupto 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.

您可以使用Popup控件。将 a 附加Popup到数据网格的当前行,可能当您单击按钮时是创建弹出窗口的好地方,并将其作为您所在行中一个单元格的子项放置。
然后您可以将用户控件添加到弹出窗口然后“显示”弹出窗口。这样,弹出窗口和用户控件的 DataContext 是从数据网格中的包含行继承的。

Below is a very simple implementation of a Popupcontrol just using XAML:

以下是Popup仅使用 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.

弹出窗口包含在堆栈面板中,但只有IsOpen在选中复选框时才可见。您可以将您的用户控件放在弹出窗口中,我在其中放置了边框和文本块。由于弹出窗口是堆栈面板的成员,如果它没有自己的堆栈面板,它会自动使用堆栈面板的 DataContext。

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

在您的实例中,我显示的堆栈面板类似于您的 DataGrid 行。

回答by user3164339

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.

好吧,我只想对 Gate Lumas 的评论发表评论,但系统不会让我发表评论,所以我将其作为答案。

XAML Popup sample: archive of link| original link

XAML 弹出示例:链接存档| 原始链接

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.

如果您下载该示例并查看 Senario1 的 .xaml 和 .cs 文件,您将获得一个关于如何使用和实现弹出窗口的完美示例。或者,您可以浏览网站上的代码,但我发现能够运行示例并与示例进行交互比仅查看代码更有帮助。