简体   繁体   English

wpf:如何弹出用户控件?

[英]wpf: How popup a usercontrol?

Background: I have a project which use the datagrid to display the data, and the datagrid has a rowdetail column which include a usercontrol. 背景:我有一个使用datagrid显示数据的项目,datagrid有一个rowdetail列,其中包含一个usercontrol。 The usercontrol has some TextBox for user inputing and displaying some message. usercontrol有一些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. 问题:我想在单击按钮时弹出usercontrol,并且popuped usercontrol与datagrid的rowdetail列中的usercontrol具有相同的上下文。 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. 这样做的目的是使用户易于与用户控件交互,因为rowdetail单元的空间是有限的。

The usecontrol has be implemente and it can work normally in the rowdetail cell. usecontrol已实现,它可以在rowdetail单元格中正常工作。 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? 但是,我不知道如何在不改变其上下文的情况下弹出它,例如数据源,消息已经在TextBox中显示等等。任何人都可以给我一些建议吗? By the way, I use the MVVM pattern. 顺便说一句,我使用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): 这是usercontrol(上面代码中的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>

You can use the Popup control. 您可以使用Popup控件。 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. Popup窗口附加到数据网格的当前行,可能是当您单击按钮是创建弹出窗口的好地方并将其作为您所在行中的一个单元格的子项放置时。
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. 这样,弹出窗口的DataContext以及usercontrol的DataContext继承自datagrid中的包含行。

Below is a very simple implementation of a Popup control just using XAML: 下面是一个使用XAML的Popup控件的一个非常简单的实现:

<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. 弹出窗口包含在stackpanel中,但只有在选中复选框时才会显示IsOpen You would place your usercontrol in the popup, where I have put the border and textblock. 你可以将你的usercontrol放在弹出窗口中,我放置了边框和文本块。 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. 由于弹出窗口是stackpanel的成员,因此如果它没有自己的一个,它会自动使用stackpanel的DataContext。

In your instance the stackpanel I am showing would be analogous to your DataGrid row. 在您的实例中,我显示的stackpanel将类似于您的DataGrid行。

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 | XAML Popup示例: 链接存档 | 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. 如果您下载该示例并查看Senario1的.xaml和.cs文件,您将拥有一个如何使用和实现弹出窗口的完美示例。 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. 或者,您可以浏览网站上的代码,但我发现能够运行和与示例交互更有帮助,而不仅仅是查看代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM