Hi Roger, for binding properties from ViewModel to properties in UserControl you must use DependencyProperties and the correct DataContext. Try following demo.
XAML MainWindow:
<Window x:Class="Window011"
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"
xmlns:local="clr-namespace:WpfApp1.WpfApp011"
xmlns:uc="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"
mc:Ignorable="d"
Title="Window011" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Window.Resources>
<local:TextToIntConverter x:Key="conv1"/>
</Window.Resources>
<StackPanel>
<Border BorderBrush="Gray" BorderThickness="3" Margin="5">
<StackPanel>
<!-- DataPicker and TextBox for binding test-->
<DatePicker SelectedDate="{Binding DayValue}" Margin="5"/>
<TextBox x:Name="tb" Text="1" Margin="5"/>
</StackPanel>
</Border>
<Border BorderBrush="Red" BorderThickness="3" Margin="5">
<uc:Window011UC1 LblDay="{Binding DayValue}" LblMonth="{Binding Text, ElementName=tb, Converter={StaticResource conv1}}" />
</Border>
</StackPanel>
</Window>
ViewModel:
Imports System.Globalization
Namespace WpfApp011
Public Class ViewModel
Public Property DayValue As Date = Now
Public Property MonthValue As Integer = 5
End Class
Public Class TextToIntConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
Dim n As Integer
If Integer.TryParse(value?.ToString, n) Then Return n
Return 0
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Return value?.ToString
End Function
End Class
End Namespace
XAML UserControl:
<UserControl x:Class="Window011UC1"
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:WpfControlLibrary1"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<local:Window011UC1Converter x:Key="conv2"/>
</UserControl.Resources>
<StackPanel x:Name="sp">
<Border BorderThickness="1"
BorderBrush="Gray">
<DockPanel>
<StackPanel x:Name="pnlDay"
DockPanel.Dock="Top"
Orientation="Horizontal">
<Label Content="{Binding LblDay}"
FontSize="16"
FontWeight="DemiBold"
HorizontalAlignment="Left">
<Label.Style>
<Style TargetType="Label">
<Setter Property="Background" Value="LightPink"/>
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource conv2}">
<Binding RelativeSource="{RelativeSource self}" Path="DataContext.LblDay" Mode="OneWay" />
<Binding RelativeSource="{RelativeSource self}" Path="DataContext.LblMonth" Mode="OneWay"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Background" Value="LightGreen"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</StackPanel>
</DockPanel>
</Border>
<!-- Datepicker for tests-->
<DatePicker SelectedDate="{Binding LblDay}"/>
</StackPanel>
</UserControl>
CodeBehind UserControl:
Imports System.Globalization
Public Class Window011UC1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
sp.DataContext = Me
End Sub
Public Shared ReadOnly LblDayProperty As DependencyProperty =
DependencyProperty.RegisterAttached(
"LblDay",
GetType(Date),
GetType(Window011UC1),
New UIPropertyMetadata(Now))
Public Shared Function GetLblDay(tvi As TreeViewItem) As Date
Return CType(tvi.GetValue(LblDayProperty), Date)
End Function
Public Shared Sub SetLblDay(tvi As TreeViewItem, value As Date)
tvi.SetValue(LblDayProperty, value)
End Sub
Public Shared ReadOnly LblMonthProperty As DependencyProperty =
DependencyProperty.RegisterAttached(
"LblMonth",
GetType(Integer),
GetType(Window011UC1),
New UIPropertyMetadata(0))
Public Shared Function GetLblMonth(tvi As TreeViewItem) As Integer
Return CType(tvi.GetValue(LblMonthProperty), Integer)
End Function
Public Shared Sub SetLblMonth(tvi As TreeViewItem, value As Integer)
tvi.SetValue(LblMonthProperty, value)
End Sub
End Class
Public Class Window011UC1Converter
Implements IMultiValueConverter
Public Function Convert(values() As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IMultiValueConverter.Convert
Dim p1 As Date
If values.Count <> 2 OrElse Not Date.TryParse(values(0).ToString, p1) Then Exit Function
Dim p2 As Integer
If Not Integer.TryParse(values(1).ToString, p2) Then Exit Function
Return p1.Month = p2
End Function
Public Function ConvertBack(value As Object, targetTypes() As Type, parameter As Object, culture As CultureInfo) As Object() Implements IMultiValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class