I have a taskbar “UserHomeTaskBar” embedded in parent window “WindowUserHomeView”, I want to use x:bind for binding the taskbar’s date/time text to parent window’s view model “WindowUserHomeViewModel”, how to implement this, thanks.
taskbar.xaml:
<?xml version="1.0" encoding="utf-8"?>
<UserControl
x:Class="Controls.Taskbar"
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"
mc:Ignorable="d">
<Border Height="60" Opacity="0.9" Background="{ThemeResource AccentAcrylicBackgroundFillColorBaseBrush}">
<Grid Height="44" VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="4" Margin="8 0">
<Grid VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{?Binding/x:bind TimeText?}" Style="{StaticResource TaskbarTextStyle}"/>
<TextBlock Text="{?Binding/X:bind DateText?}" Style="{StaticResource TaskbarTextStyle}"/>
</Grid>
</Button>
</Grid>
</Border>
Taskbar.xaml.cs:
namespace Controls
{
public partial class Taskbar : UserControl
{
public Taskbar()
{
this.InitializeComponent();
}
}
}
WindowUserHomeView.xaml:
<?xml version="1.0" encoding="utf-8"?>
<Page
x:Class="Views.UserHome.WindowUserHomeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Views.UserHome"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:resourceModels ="using:DataAccess.Models.Resources"
xmlns:controls="Controls"
mc:Ignorable="d">
<Border Background="{x:Bind ViewModel.CurrentWallpaper.Color,Mode=OneWay}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid x:Name="userhomeWindow" Height="700" Grid.Row="0">
<Grid.Background>
<ImageBrush Stretch="Fill" ImageSource="{x:Bind ViewModel.CurrentWallpaper.ImagePath,Mode=OneWay}" />
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
<controls:Taskbar Grid.Row="1" x:Name="UserHomeTaskBar"/>
</Grid>
</Border>
</Page>
WindowUserHomeViewModel.cs:
namespace ViewModels.UserHome
{
public class WindowUserHomeViewModel: ObservableObject, IViewModelBase
{
private string _timeText;
public string TimeText
{
get { return _timeText; }
set { _timeText = value; OnPropertyChanged(); }
}
private string _dateText;
public string DateText
{
get { return _dateText; }
set { _dateText = value; OnPropertyChanged(); }
}
public WindowUserHomeViewModel()
{
InitializeDateTime();
}
}
}