Changing content dynamically in wpf window - wpf

What i want to do is change/slide the content of a wpf window on the click of a button. I am new to wpf and have no clue how to do this. Please, if anyone can help me, I will be so grateful. Any video tutorials would be best.

You can put the content of the window into a UserControl. Your window then only has a content-control and a button to change the content. On a click on the button you can reassign the content-property of the content-control.
I've made a small example for this.
The XAML-Code for your MainWindow can look like this:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Content="Switch" Click="ButtonClick"/>
<ContentControl x:Name="contentControl" Grid.Row="1"/>
</Grid>
</Window>
I've added two UserControls to the solution. The CodeBehind for the MainWindow looks like:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.contentControl.Content = new UserControl1();
}
private void ButtonClick(object sender, RoutedEventArgs e)
{
this.contentControl.Content = new UserControl2();
}
}
Update
I've created a small usercontrol called MyUserControl. The xaml-markup looks like
<UserControl x:Class="WpfApplication.MyUserControl"
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="300">
<StackPanel Orientation="Vertical">
<Label Content="This is a label on my UserControl"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<Button Content="Testbutton 1" Margin="5"/>
<Button Content="Testbutton 2" Margin="5"/>
</StackPanel>
<CheckBox Content="Check Me"/>
</StackPanel>
</UserControl>
In the button-click-event above you can assign a new instance of this usercontrol to the content-control. This you can do by:
this.contentControl.Content = new MyUserControl();

Related

How to retrieve data from a page in a frame on button click c#

private void btnEdit(object sender, RoutedEventArgs e)
{
}
in this button event i want to be able to retrieve two variables from a different xaml which is displayed in the frame of this window. I have no clue how to do this
I have a mainscreen with two buttons and a frame. in this frame i display a page. when this button in the above code is clicked i want to be able to get two variables from the pages textblock on the page. how would i go about this?
i basically need to collect data from the frame to be used in this event
I don't know that your project structure and what you try to do. So I would show you an example on the basis of I understood.
I created the below Page. Page name is "TestPage.xaml"
<Page x:Class="StackOverFlowAnswers.TestPage"
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"
xmlns:local="clr-namespace:StackOverFlowAnswers"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="TestPage">
<Grid>
<TextBlock x:Name="textBlock"/>
</Grid>
</Page>
I created MainWindow that has two button with one frame as below.
Now the source of the Frame is "TestPage.xaml".
<Window x:Class="StackOverFlowAnswers.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:attached="clr-namespace:Parse.WpfControls.AttachedProperties"
xmlns:local="clr-namespace:StackOverFlowAnswers"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style x:Key="ConvertableTextBox" TargetType="TextBox">
<Setter Property="attached:CharacterConvertBehavior.ConvertEnable" Value="True"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0">
<Button Width="50" Margin="0 0 10 0" Click="Button_Click"/>
<Button Width="50" Margin="0 0 10 0"/>
</StackPanel>
<Grid Grid.Row="1">
<Frame x:Name="mainFrame" Source="TestPage.xaml">
</Frame>
</Grid>
</Grid>
</Window>
Now when the button is clicked would move to the event method of the below.
private void Button_Click(object sender, RoutedEventArgs e)
{
var page = (TestPage)this.mainFrame.Content;
page.textBlock.Text = "test!!!";
}
In the above way, you could access (retrieve) the var of the Page.

WPF copying wrap panel

How do I copy WrapPanel?
I have window in which I have WrapPanel with some controls inside. I'd like to copy it inside my code so I would have 2 or more exact looking panels on the same window but with different names for controls.
This is how my XAML looks like:
<Window x:Class="WpfApplication1.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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel x:Name="stackPanel" HorizontalAlignment="Left" Height="301" Margin="10,10,0,0" VerticalAlignment="Top" Width="498">
<WrapPanel x:Name="Wrap1" Height="142" Margin="0,0,345.6,0" VerticalAlignment="Top" Width="152">
<Button x:Name="wrap1_button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
<Button x:Name="wrap1_button1" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
<Label x:Name="wrap1_label" Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top" Width="74"/>
</WrapPanel>
</StackPanel>
</Grid>
I would like to get another WrapPanel placed next to first one, named Wrap2 with controls inside wrap2_button, wrap2_button1, wrap2_label.
I tried inserting it in StackPanel and doing something like that:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
WrapPanel Wrap2 = Wrap1;
stackPanel.Children.Add(Wrap2);
}
}
but it doesnt work...
In short, you will have to create another WrapPanel, add it to the StackPanel, and add children to the second WrapPanel.
Now, if your reaction is to recoil from the duplication, that is a good instinct. This may be a good use case for a UserControl which is a way to bundle groups of standard WPF controls to allow for their reuse.
What I would suggest is to create a new UserControl which is a WrapPanel with the children you have, then add 2 instances of that new UserControl to the StackPanel in your window.
Here is a link to the Remarks of the UserControl class with some of the considerations of creating your own custom controls.

Orientation attribute of StackPanel defined as top level component in separate xaml file ignored

I am defining a StackPanel in a file called HPanel.xaml like this:
<StackPanel Orientation="Horizontal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
Then, using the HPanel like this:
<v:HPanel>
<Label Content="The label" />
<TextBox Text="{Binding transporter.Foo,
UpdateSourceTrigger=PropertyChanged}" />
</v:HPanel>
Will give me a stackpanel with vertical orientation instead of horizontal.
Why?
Tada!
I have made a working test case now, the stackpanel becomes nice if I have a code behind. InitializeComponent was needed for attributes to be processed.
MainWindow.xaml:
<Window x:Class="wpftestapp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:v="clr-namespace:wpftestapp.views"
Title="MainWindow" Height="350" Width="525">
<v:HPanel>
<Label Content="a"/>
<Label Content="b"/>
<Label Content="c"/>
</v:HPanel>
</Window>
HPanel.xaml:
<StackPanel Orientation="Horizontal"
x:Class="wpftestapp.views.HPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"/>
HPanel.xaml.cs:
using System.Windows.Controls;
namespace wpftestapp.views
{
public partial class HPanel : StackPanel
{
public HPanel()
{
InitializeComponent();
}
}
}
Thanks for the help!

Access to PreviewKeyDown event of richTextBox in userControl

I would like to know how can I get access to event PreviewKeyDown of RichTextBox in UserControl.
For example, I have user Control and in this user control I have only one richTextBox:
Something like this:
<UserControl x:Class="Spirit.Controls.RichTextBoxControl"
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"
xmlns:toolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<toolkit:RichTextBox Name="RichTextBox"
Grid.Row="0" PreviewKeyDown="?">
</toolkit:RichTextBox>
</Grid>
</UserControl>
I use this control in WPF window.
<Window x:Class="WpfApplication2.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:WpfApplication2.Controls" xmlns:WpfApplication2="clr-namespace:WpfApplication2" Title="Window2" Height="300" Width="300">
<Grid>
<Spirit.Controls:RichTextBoxControl Background="Red"
FontSize="13"
Margin="4,4,4,4"
Grid.Row="0"
Here I would like to acces to PreviewKeyDown of richTextBox/>
</Grid>
</Window>
I would like have access to PreviewKeyDown of richTextBox, bind some method on this event and have access to KeyEventArgs.
Something like this:
private void RichTextBoxInUserControl_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter )
{
//...
}
}
I noticed the Intellisence wasn't picking up on RichTextBox... in Window but you can subscribe to that event like this
<Spirit.Controls:RichTextBoxControl
Name="RichTextBoxInUserControl"
Background="Red"
FontSize="13"
Margin="4,4,4,4"
Grid.Row="0"
RichTextBox.PreviewKeyDown="RichTextBoxInUserControl_PreviewKeyDown"/>
where RichTextBox is the Name of the toolkit:RichTextBox specified in your UserControl

how to put array of user control in the columns of Grid

Hi I have array of Employee data which includes photo,description, title name,salary. I have to show each employee data in separate panel control structure that means if I have 10 employees there will be 10 panels. and I have to show these panels in a grid which has two columns. Also width of the panel in each column will vary according to main page size.
Why not represent the employee data with a separate userControl then use a Wrap panel to display the employees. This approach would handle a variable number of employees.
To better illustrate my idea here's some code:
The MainPage has a grid with a WrapPanel that fills the available space.
MainPage.xaml
<UserControl
x:Class="SilverlightApplication2.MainPage"
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:ct="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
mc:Ignorable="d">
<Grid x:Name="LayoutRoot" Background="WhiteSmoke">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<ct:WrapPanel x:Name="panel1" Grid.Row="1" Grid.Column="1"
Background="Aqua" VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"/>
</Grid>
</UserControl>
MainPage.xaml.cs
public partial class MainPage : UserControl
{
public MainPage ()
{
InitializeComponent();
this.DataContext = this;
this.Loaded += new RoutedEventHandler( MainPage_Loaded );
}
void MainPage_Loaded ( object sender, RoutedEventArgs e )
{
for ( int x = 0; x <= 10; x++ )
{
panel1.Children.Add( new ChildControl() );
}
}
}
I am adding ChildWindow controls as defined below to show how the WrapPanel adapts the presentation of its children to the space available.
ChildWindow.xaml
<UserControl
x:Class="SilverlightApplication2.ChildControl"
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"
Width="100"
Height="100">
<Grid x:Name="LayoutRoot" Background="PowderBlue">
<TextBlock Text="ChildControl"/>
</Grid>
</UserControl>
If I've missed your point please give some more clarification.

Resources