WPF: Selection enter open and well take over with Button Elements
This example shows that we open a WPF element a selection as a window and a selection is made from it.
The selection then adds a button to the inside of the first caller
Call pop-up
The window is created and displayed via a new window (..).
The trick here is that as a window (argument) the caller is handed over.
private void Panel_row_1_col_1_MouseUp(object sender, MouseButtonEventArgs e) { Border panel = sender as Border; Window_Select popup = new Window_Select(panel); popup.ShowDialog(); }
|
Aufruf
Trick im Popup-Window
The trick here is that in the window that is called, a call method of the same name () is created, which contains a WPF element as a parameter.
This allows you to give a parameter to a window when you create
public Window_Select(Border srcPanel) { //< get caller_Element > _srcPanel = srcPanel; //</ get caller_Element >
InitializeComponent(); }
|
Take over selection
If the selection is selected, then the call item held in memory is addressed directly.
Since the calling element, like here, a border item has been adopted as a Windows (source argument), it can be used here.
Here is in the element with. Child placed in the calling element a button.
Button btn = new Button(); btn.Background = new SolidColorBrush(Colors.Green);
_srcPanel.Child = btn;
|
Then the window becomes geschlossen
this.Close();
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes;
namespace Demo_InsertElement { /// <summary> /// Interaction logic for Window_Select.xaml /// </summary> public partial class Window_Select : Window { private Border _srcPanel;
#region Form //=============< region: Form >============= public Window_Select(Border srcPanel) { //< get caller_Element > _srcPanel = srcPanel; //</ get caller_Element >
InitializeComponent(); } //=============</ region: Form >============= #endregion
#region Buttons //=============< region: Buttons >============= private void BtnRed_Click(object sender, RoutedEventArgs e) { //------------< BtnRed_Click() >------------ //< create element > Button btn = new Button(); btn.Background = new SolidColorBrush(Colors.Red); //</ create element >
//< element into caller > //*adds a new element into the caller _srcPanel.Child = btn; //</ element into caller >
//< close > this.Close(); //</ close > //------------</ BtnRed_Click() >------------ }
private void BtnGreen_Click(object sender, RoutedEventArgs e) { Button btn = new Button(); btn.Background = new SolidColorBrush(Colors.Green); _srcPanel.Child = btn; this.Close(); } //=============</ region: Buttons >============= #endregion } }
|
XAML
Basic window
The base window that opens a pop-up window as a selection.
In this case, the red rectangle of the type Border should open a window.
If the selection window has been opened, then a button should be placed in the border element as an element. The same can, of course, be created for other WPF elements.
Importantly, a caller element opens a selection window
<Window x:Class="Demo_InsertElement.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Demo_InsertElement" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="186*"/> <ColumnDefinition Width="53*"/> <ColumnDefinition Width="555*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="98*"/> <RowDefinition Height="52*"/> <RowDefinition Height="269*"/> </Grid.RowDefinitions> <Border x:Name="panel_row_1_col_1" BorderBrush="Red" BorderThickness="1" Margin="2" Grid.Row="1" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" MouseUp="Panel_row_1_col_1_MouseUp" Background="#f0f0f0" />
</Grid> </Window>
|
The pop-up window
As a selection window for data
A button is
selected here in the call window.
It is important that this triggers an event that represents a selection and
thus the value can be returned to the WPF caller.
<Window x:Class="Demo_InsertElement.Window_Select" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Demo_InsertElement" mc:Ignorable="d" Title="Window_Select" Height="450" Width="800"> <Grid> <Label Content="Select a Button" HorizontalAlignment="Left" Height="46" Margin="44,26,0,0" VerticalAlignment="Top" Width="257"/> <Button x:Name="BtnRed" Content="Red" Height="51" Margin="64,69,0,0" Width="89" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Red" Click="BtnRed_Click"/> <Button x:Name="BtnGreen" Content="Green" Height="51" Margin="65,133,0,0" Width="89" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Green" Click="BtnGreen_Click"/>
</Grid> </Window>
|