Hi, to use assign in code try following demo:
<Page
x:Class="App1.Page03"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App03"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<Border BorderBrush="Green" BorderThickness="3" Margin="5">
<StackPanel>
<TextBlock Text="Text for UserControl"/>
<TextBox x:Name="tb" Text="first title" TextChanged="tb_TextChanged"/>
<TextBlock Text="Switch to show UserControl"/>
<CheckBox x:Name="cb" IsChecked="True"/>
</StackPanel>
</Border>
<Border BorderBrush="LightGreen" BorderThickness="3" Margin="5">
<StackPanel>
<TextBlock Text="UserControl"/>
<Border BorderBrush="Red" BorderThickness="3" Margin="5">
<local:UserControlFacilitiesSummary x:Name="uc"/>
</Border>
</StackPanel>
</Border>
</StackPanel>
</Page>
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace App1
{
public sealed partial class Page03 : Page
{
public Page03()
{
this.InitializeComponent();
this.uc.TitleText = this.tb.Text;
this.uc.ShowTitleText = this.cb.IsChecked.Value;
this.cb.Checked += cb_Checked_Unchecked;
this.cb.Unchecked += cb_Checked_Unchecked;
}
private void tb_TextChanged(object sender, TextChangedEventArgs e) => this.uc.TitleText = this.tb.Text;
private void cb_Checked_Unchecked(object sender, RoutedEventArgs e) => this.uc.ShowTitleText = this.cb.IsChecked.Value;
}
}
<UserControl
x:Class="App03.UserControlFacilitiesSummary"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App03"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>
<local:BooleanToVisibilityConverter x:Key="b2vconv"/>
</UserControl.Resources>
<Grid x:Name="grd">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding TitleText}" Visibility="{Binding ShowTitleText, Converter={StaticResource b2vconv}}"/>
<Border Grid.Row="1" BorderBrush="Green" BorderThickness="3">
<TextBlock Text="Body"/>
</Border>
</Grid>
</UserControl>
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
namespace App03
{
public sealed partial class UserControlFacilitiesSummary : UserControl
{
public UserControlFacilitiesSummary()
{
this.InitializeComponent();
this.grd.DataContext = this;
}
public static readonly DependencyProperty TitleTextProperty =
DependencyProperty.Register("TitleText", typeof(string), typeof(UserControlFacilitiesSummary),
new PropertyMetadata(""));
public static readonly DependencyProperty ShowTitleTextProperty =
DependencyProperty.Register("ShowTitleText", typeof(bool), typeof(UserControlFacilitiesSummary),
new PropertyMetadata(true));
public string TitleText
{
get => GetValue(TitleTextProperty).ToString();
set { SetValue(TitleTextProperty, value); }
}
public bool ShowTitleText
{
get => (bool)GetValue(ShowTitleTextProperty);
set { SetValue(ShowTitleTextProperty, value); }
}
}
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language) =>
(bool)value ? Visibility.Visible : Visibility.Collapsed;
public object ConvertBack(object value, Type targetType, object parameter, string language) =>
(Visibility)value == Visibility.Visible;
}
}