I'm trying to bind passing the custom object of type "CommandButton_VModel" to an User control through a dependency property, but it seems the setter of this dp is never hit. I mean at the user control the dependency property is always null, could someone please advise-me on where am I failing?
Here's my code below:
UserControl XAML:
<UserControl x:Class="FireAntTempConfigurer.UserControls.CommandButton_UC"
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:FireAntTempConfigurer.UserControls"
xmlns:materialDesign="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
mc:Ignorable="d"
TextElement.Foreground="{DynamicResource MaterialDesignPaper}"
d:DesignHeight="60 " d:DesignWidth="60">
<Grid>
<Button Height="50" Padding="0,0,0,0" Margin="5" ToolTip="{Binding PlaceHolder}">
<Button.Content>
<materialDesign:PackIcon Kind="{Binding Icon}" Height="50" Width="50" />
</Button.Content>
</Button>
</Grid>
</UserControl>
UserControl Code Behind
public partial class CommandButton_UC : UserControl
{
public CommandButton_UC()
{
DataContext = CmdBtn;
InitializeComponent();
}
public CommandButton_VModel CmdBtn
{
get { return (CommandButton_VModel)GetValue(CmdBtnProperty); }
set { SetValue(CmdBtnProperty, value); }
}
public static readonly DependencyProperty CmdBtnProperty =
DependencyProperty.Register("CmdBtn", typeof(CommandButton_VModel), typeof(CommandButton_UC), new PropertyMetadata(default(CommandButton_VModel)));
}
MainWindow XAML:
<StackPanel>
<TextBlock>ToolBox:</TextBlock>
<Separator></Separator>
<StackPanel Orientation="Horizontal">
<cmdBtn:CommandButton_UC CmdBtn="{Binding MyProperty ,Mode=OneWayToSource}">
</cmdBtn:CommandButton_UC>
</StackPanel>
</StackPanel>
MainWindow Code
public partial class MainWindow : Window
{
public CommandButton_VModel MyProperty { get; set; }
public MainWindow()
{
MyProperty = new CommandButton_VModel { Icon = "Plus", PlaceHolder = "Add" };
DataContext = MyProperty;
InitializeComponent();
}
}
ViewModel
public class CommandButton_VModel : INotifyPropertyChanged
{
public string Icon { get; set; }
public string PlaceHolder { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public CommandButton_VModel()
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Icon"));
PropertyChanged(this, new PropertyChangedEventArgs("PlaceHolder"));
}
}
}
Thanks!