wpf:如何弹出用户控件?

Gat*_*uma 6 c# wpf user-controls datagrid mvvm

背景:我有一个使用datagrid显示数据的项目,datagrid有一个rowdetail列,其中包含一个usercontrol.usercontrol有一些TextBox供用户输入和显示一些消息.

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

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

编辑: 这是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>
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

Ste*_*bob 7

您可以使用该Popup控件.将a附加Popup到datagrid的当前行,可能是当您单击该按钮是创建弹出窗口的好地方并将其作为您所在行中的一个单元格的子项时.
然后您可以将usercontrol添加到弹出窗口中然后'显示'弹出窗口.这样,弹出窗口的DataContext以及usercontrol的DataContext继承自datagrid中的包含行.

下面是一个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>
Run Code Online (Sandbox Code Playgroud)

弹出窗口包含在stackpanel中,但仅IsOpen在选中复选框时才可见.你可以将你的usercontrol放在弹出窗口中,我放置了边框和文本块.由于弹出窗口是stackpanel的成员,因此如果它没有自己的一个,它会自动使用stackpanel的DataContext.

在您的实例中,我显示的stackpanel将类似于您的DataGrid行.