0

As one with very little to no clue at WPF....

I am trying to switch user controls. currently my code is using "new" to get the user control object but it is very problematic cause this code is being read lot of times and the objects are not released ,I am using very weak hardware.

Is there any other way to switch between user controls?

I have xaml file thats look like that

<UserControl x:Uid="UserControl_1" x:Class="ddd.WindowFooter"
             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:ddd"
             xmlns:Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:Core="clr-namespace:Microsoft.Expression.Interactivity.Core" 
             xmlns:ddd="clr-namespace:ddd" 
             xmlns:Core1="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             DataContext="{Binding Source={StaticResource _indicatorSelector}}" KeyboardNavigation.TabNavigation="None">

    <Border x:Uid="Border_1" HorizontalAlignment="Left" VerticalAlignment="Bottom" CornerRadius="0,10,0,0" Background="{Binding Background}">
        <StackPanel x:Uid="StackPanel_1"  Margin="10,10,10,0">
            <Grid x:Uid="Grid_1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition x:Uid="ColumnDefinition_1" />
                <ColumnDefinition x:Uid="ColumnDefinition_2" />
            </Grid.ColumnDefinitions>

            <ContentControl x:Uid="contentControl1" Content="{Binding Slot1Control}" MouseDown="DoMouseDown"
                Height="40" Name="contentControl1" Width="100"  Grid.Column="0" />

                <ContentControl x:Uid="contentControl2" Content="{Binding Slot2Control}"  MouseLeftButtonUp="DoMouseDown" 
            </Grid>

        <Grid x:Uid="Grid_2" Grid.Row="1" Width="200" Height="22" Margin="0,7" HorizontalAlignment="Left" VerticalAlignment="Bottom">
            <Viewbox x:Uid="Viewbox_1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Uniform">
                <ContentControl x:Uid="ContentControl_1" HorizontalAlignment="Left" VerticalAlignment="Top" ContentTemplate="{StaticResource SomeImage}" />
            </Viewbox>
        </Grid>
        </StackPanel>
    </Border>
</UserControl>

And CS thats looks like

public class IndicatorSelector : INotifyPropertyChanged
{
    UserControl Slot1Control{
        get
        {
            return new Slot1UserControl();
        }
    }

    UserControl Slot2Control{
        get
        {
            return new Slot2UserControl();
        }
    }
}

Thanks

  • 2
    Create private field `UserControl _slot1Control = new Slot1UserControl()` and them return _slot1control instead of new object – sTrenat Jul 31 '18 at 15:42
  • Also, if you looking for performance you could replace grid with dockpanel, it's little faster panel and could do exactly the same in your case. I guess that probably even stackPanel with horizontal orientation could. – sTrenat Jul 31 '18 at 15:46
  • @sTrenat thanks but the problem is that the UserControl_1 is used in diffrent windows and I am getting error on object is used and need to disconnect it... – Eran Tenenboim Aug 01 '18 at 05:37
  • Think it is duplicate with "Binding a ContentControl to UserControl, and reuse same instance " – Eran Tenenboim Aug 01 '18 at 05:38
  • You could create method `void RemoveElementFromItsParent(FrameworkElement el) { if(el.Parent==null) return; switch (el.Parent) { case ContentControl cC: cC.Content = null; break; case Panel p: p.Children.Remove(el); break; case ContentPresenter cP: cP.Content = null; break; case Decorator d: d.Child = null;break; } }` and then call it like, before get return `RemoveElementFromItsParent(_slot1Control)` – sTrenat Aug 01 '18 at 20:30

0 Answers0