If anyone knows the solution to create dynamic xaml using data related (DataContext="{Binding User}") or static data (DataContext="{Binding User, Source={StaticResource Locator}}")?
Such a solution could be saved on the controls hidden and additional fields (Visibility="{Binding IsAnonim, Converter={StaticResource VisibilityConverter}}"). And XAML had to only what is needed in this case and was not saturated with unnecessary controls.
Code ("if (IsUser) { }") that could be exercised at moment of rendering.
Example below:
<UserControl
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"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" DataContext="{Binding User}">
<StackPanel>
<? if (IsUser) { ?>
<TextBox Text="{Binding UserName, Mode=TwoWay}" />
<PasswordBox Text="{Binding UserPass, Mode=TwoWay}" />
<? } else if (IsAnonim) { ?>
<TextBox Text="{Binding AnonimName, Mode=TwoWay}" />
<? } ?>
<TextBox Text="{Binding MsgText, Mode=TwoWay}" />
<Button Content="Button" Command="{Binding PostCommand}" />
</StackPanel>
</Grid> </UserControl>
P.S. Other solutions are possible. Thank you.
Userproperty, you may use different DataTemplates and set theirDataTypeappropriately. Otherwise use a ContentControl with an appropriate DataTemplateSelector in itsContentTemplateSelectorproperty. You may take a look at the Data Templating Overview article on MSDN. - Clemens