WPF:如何弹出一个用户控件? [英] wpf: How popup a usercontrol?

查看:196
本文介绍了WPF:如何弹出一个用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:
我有使用DataGrid来显示数据的项目,数据网格有一个rowdetail列,其中包括一个用户控件。用户控件有一些文本框用户输入查询并显示一些消息。

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.

问题:
我想使用户控件弹出,当我点击一个按钮,popuped用户控件具有相同背景下的用户控件的中的rowdetail列数据网格。这样做的目的是让用户轻松互动的用户控件,因为rowdetail细胞的那个房间是有限的。

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.

usecontrol有是implemente,它可以在rowdetail细胞正常工作。不过,我不知道如何流行起来,而不改变它的上下文的主意,比如数据源,消息已经在文本框中显示了,等等。任何人都可以给我一些建议?
顺便说一句,我使用MVVM模式

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.

编辑:
这里是RowDetailTemplate:

Here is the 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>

下面是该用户控件(在上述代码的MyUserControl):

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

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



推荐答案

您可以使用弹出控制。附加弹出您DataGrid的当前行,可能是当您单击按钮来创建弹出并把它作为细胞之一,在一个孩子的好地方排你在。结果
然后你可以将用户控件添加到弹出,然后显示弹出。这样一来,在DataContext为弹出式窗口,从而为用户控件从DataGrid中包含行继承。

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.

下面是一个非常简单的实现<$ C $的C>弹出控件中使用XAML:

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>



弹出包含在StackPanel的,但仅仅是可见的, ISOPEN ,当复选框被选中。你会把你的用户控件,在弹出的,在那里我已经把边框和文字块。
由于弹出是StackPanel中的一员,它会自动使用的StackPanel的DataContext的,如果它不具有其自己的一个

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.

在您的实例在StackPanel中我显示就类似于你的DataGrid行。

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

这篇关于WPF:如何弹出一个用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆