I’m working on a project with several UserControl
elements and MVVM
. The task is to create a dynamic user interface to prevent using any pop-up windows with Show()
or ShowDialog()
.
EDIT: After several comments (thanks for the feedback) and checking the flow of the text, I decided to edit the text and try to explain more about the task.
In a previous version of this tool everything was handled by separate dialogs and therefore it was possible to create a mess on the screen (not recommended, but possible). The tool is some kind of control center and can dynamically load other sub-modules. So, in the new version my idea was to add a UserControl
that can handle two tasks at once: 1. Be some kind of overview for the possible number of sub-modules, 2. Be the receiver of the sub-module UserControls
.
I started the test of this construction in my SettingsView and added my new UserControl
which hopefully was most dynamic. Image1 is to show the idea behind.
SettingsView.xaml:
<UserControl x:Class="nameXY.Views.SettingsView"
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:nameXY.View"
xmlns:vm="clr-namespace:nameXY.ViewModel"
mc:Ignorable="d"
d:DesignHeight="850" d:DesignWidth="500"
Background="White">
<UserControl.DataContext>
<vm:SettingsViewModel />
</UserControl.DataContext>
<!-- some code -->
<UserControl Grid.Column="0"
x:Name="ModuleControl">
<local:CommonUserControlCollectionView/>
</UserControl>
</UserControl>
Loading this empty UserControl
is fine, but filling the content is currently not working. The same UserControl
should be able to receive other user controls afterwards in another part of the tool. Those controls would come from external DLLs, but the idea is the same as in the image above.
My original CommonUserControlCollectionView.xaml is:
<UserControl x:Class="nameXY.View.CommonUserControlCollectionView"
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:nameXY.View"
xmlns:vm="clr-namespace:nameXY.ViewModel"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.DataContext>
<vm:CommonUserControlCollectionViewModel />
</UserControl.DataContext>
<Grid>
<ScrollViewer VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Disabled">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<UserControl Grid.Row="0"
x:Name="UserControlPanel01"
MinHeight="30"/>
<UserControl Grid.Row="2"
x:Name="UserControlPanel02"
MinHeight="30"/>
<UserControl Grid.Row="4"
x:Name="UserControlPanel03"
MinHeight="30"/>
<UserControl Grid.Row="6"
x:Name="UserControlPanel04"
MinHeight="30"/>
<UserControl Grid.Row="8"
x:Name="UserControlPanel05"
MinHeight="30"/>
</Grid>
</ScrollViewer>
</Grid>
</UserControl>
As this view is embedded in the SettingsView
I then tried to add the controls dynamically in the view model.
SettingsViewModel.cs:
public CommonUserControlCollectionView ModuleControl { get; set; }
private void CallUpdateModuleList()
{
var _control01 = new ModuleActivationControlView();
((ModuleActivationControlView)_control01).InitializeView("TestControl01", false, true);
ModuleControl.UserControlPanel01.Children.Clear();
ModuleControl.UserControlPanel01.Children.Add(_control01);
}
In debug mode this works fine, but on the GUI nothing happens. Due to other questions here (like WPF add element… or Usercontrol c# add…) I started testing other approaches (as seen in my first draft) and changed the controls to StackPanel
, WrapPanel
or ContentControl
, but also there nothing changes and I’m sure I’m missing something.
I hope I was able to give a better explanation 🙂 last one was made in a hurry yesterday.
7