I have a UserControl 'UserControlA' with ViewModel 'ViewModelA'. 'UserControlA' has 'UserControlB', and 'UserControlB' has 'ViewModelB'.
When I bind a DependencyProperty in 'UserControlA' with 'ViewModelA' property, there is none of setter fired.
Belows are code,
ViewA.xaml
<UserControl
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:vm="clr-namespace:MyTest.ViewModel
xmlns:custom="clr-namespace:MyTest.Views
x:Name="userControl" x:Class="MyTest.Views.UserControlA"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="500">
<UserControl.DataContext>
<vm:UserViewModel x:Name="uvModel"/>
</UserControl.DataContext>
<Grid>
<custom:UserControlB></custom:UserControlB>
ViewA.cs
public partial class UserView : UserControl, IUserView
{
static DependencyProperty UserTypeProperty = DependencyProperty.Register("UserType", typeof(UserType), typeof(UserView), new PropertyMetadata(UserType.None));
public UserType UserType { get { return (UserType)GetValue(UserTypeProperty); } set { SetValue(UserTypeProperty, value); } }
public ViewA()
{
InitializeComponent();
Binding typeBinding = new Binding();
typeBinding.Source = this.DataContext;
typeBinding.Path = new PropertyPath("User.UserType");
typeBinding.Mode = BindingMode.OneWayToSource;
this.SetBinding(UserTypeProperty, typeBinding);
}
ViewModelA.cs
public class ViewModelA : ViewModelBase
{
User user = new User();
public User User
{
get { return this.user; }
set
{
this.user = value;
RaisePropertyChanged(() => User);
}
}
Please help me out from this problem.
has usercontrol with own viewmodel
yet I don't see that (horrible code smell) in your code, and I can't tell if your ViewModelA has a public property ViewModelB, which it should if your UserControlB is nested in your ViewModelA. - user1228