Wpf: How is the usercontrol popup?

Background: I have a project that uses a datagrid to display data, and the datagrid has a rowdetail column that includes usercontrol. The usercontrol user has some TextBox to enter and display some messages.

Problem: I want to make the usercontrol popup when I click the button, and the popup user control has the same context as usercontrol in the rowdetail column in the datagrid. This is to ensure that users can interact interactively with usercontrol, as this rowdetail cell cell is limited.

The usecontrol function is implemented and can work normally in the rowdetail cell. However, I have no idea how to pop up without changing its context, such as a data source, messages in a TextBox, etc. Can anyone give me some advice? By the way, I am using the MVVM pattern.

EDIT: Here is an example of a 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 user control (MyUserControl in the codes above):

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

+4
source share
2 answers

You can use the Popup control. Attach a Popup to the current line of your data file, probably when you click a button, this is a good place to create a popup and place it as a child of one of the cells in the line in which you are located.
Then you can add usercontrol to the popup, and then β€œshow” the popup. Thus, the DataContext for the popup and therefore for usercontrol is inherited from the contained row in the datagrid.

The following is a very simple implementation of a Popup control 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> 

A popup is enabled on the stack, but only IsOpen displayed when the checkbox is IsOpen . You would place the usercontrol in a popup where I set the border and text block. Since the popup is a member of the stack panel, it automatically uses the DataContext of the stack panel if it does not have its own.

In your instance, the stack that I am showing will be similar to your DataGrid row.

+6
source

Well, I would just like to comment on Gate Lumas comment, but the system will not allow me, so I put this as an answer.

XAML Popup sample: link archive | source link

If you download this sample and look at the .xaml and .cs files for Senario1, you will have a great example of how to use and implement the popup. Alternatively, you can simply view the code on the site, but I believe that you can work and interact with the sample, more useful than just looking at the code.

+1
source

Source: https://habr.com/ru/post/1501475/


All Articles