Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace contents WPF grid control by grid in other XAML file
    primarykey
    data
    text
    <p>I'm trying to replace the content of a WPF grid control by another WPF grid defined in a second XAML file in code (c#).</p> <p>(simplified example) Window1.xaml: </p> <pre><code>&lt;Window x:Class="Demo1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"&gt; &lt;DockPanel&gt; &lt;Menu DockPanel.Dock="Top"&gt; &lt;MenuItem Header="_Set Grid" Click="MenuItem_Click" /&gt; &lt;/Menu&gt; &lt;StatusBar DockPanel.Dock="Bottom"&gt; &lt;StatusBarItem Name="statusItem"&gt;Status&lt;/StatusBarItem&gt; &lt;/StatusBar&gt; &lt;Grid Name="header" DockPanel.Dock="Top"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock Name="txtHi" Grid.Row="0" Grid.Column="0"&gt;Hi&lt;/TextBlock&gt; &lt;TextBlock Name="txtName" Grid.Row="0" Grid.Column="1"&gt;X&lt;/TextBlock&gt; &lt;/Grid&gt; &lt;Grid Name="gridContent"&gt; &lt;/Grid&gt; &lt;/DockPanel&gt; </code></pre> <p></p> <p>Windows2.xaml contains the grid that replaces gridContent</p> <pre><code>&lt;Window x:Class="Demo1.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window2" Height="300" Width="300"&gt; &lt;Grid Name="grid2"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Label Grid.Column="1" Grid.Row="1"&gt;Hello !!!&lt;/Label&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>The MenuItem_Click event in the code behind Windows1.xaml.cs contains:</p> <pre><code>private void MenuItem_Click(object sender, RoutedEventArgs e) { Window2 win2 = new Window2(); gridContent = win2.grid2; setStatus(); } private void setStatus() { statusItem.Content = "gridContent has " + gridContent.RowDefinitions.Count + " rows and " + gridContent.ColumnDefinitions.Count + " columns."; } </code></pre> <p>Although the statusItem say the gridContent contains 2 rows and 2 columns after a click on the menu, the window is not changed and does not contain the text Hello!!!</p> <p>Any ideas what I'm doing wrong? If there are better solutions to "embed" a grid from a second xaml file, please let me know.</p> <p>Thanks, Robbie</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload