There is a UserControl, it contains several textboxes for changing class fields. So, it’s impossible to bind these class fields in two-way mode to the UserControl text boxes.

View:

<UserControl x:Class="Editor3.Modules.Views.CreateView" xmlns:ViewModels="clr-namespace:Editor3.Modules.ViewModels"> <Grid> <TextBox Text="{Binding Name, Mode=TwoWay}"/> <TextBox Text="{Binding Description}" /> <Button Content="Create" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.CreateCommand}"/> </Grid> 

ViewModel:

 namespace Editor3.Modules.ViewModels { public class CreateViewModel : ViewModelBase, INotifyPropertyChanged { public CreateViewModel() { CreateCommand = new Command(Create); } public Command CreateCommand { get; private set; } World World { get { return GetValue<World>(WorldProperty); } set { SetValue(WorldProperty, value);} } public static readonly PropertyData WorldProperty = RegisterProperty("World", typeof(World)); } } 

Model:

 namespace Library.Objects { using Catel.Data; using Catel.MVVM; public class World : ModelBase, INotifyPropertyChanged { [Model] public string Name { get { return GetValue<string>(NameProperty); } set { SetValue(NameProperty, value); } } public static readonly PropertyData NameProperty = RegisterProperty("Name", typeof(string), "Name"); [Model] public string Description { get { return GetValue<string>(DescriptionProperty); } set { SetValue(DescriptionProperty, value); } } public static readonly PropertyData DescriptionProperty = RegisterProperty("Description", typeof(string), "Description"); } } 

So pressing the button causes the correct function, i.e. binditsya normally and the fields do not want!

  • Why do you have such a complicated Binding for Button? - VladD
  • The only option that has earned. already shoveled a bunch of options - Artem Hohryakov

1 answer 1

You are misusing the Catel framework. The [Model] attribute is used for properties on the ViewModel. The ViewModel must have the Name and Description properties. Re-read the documentation again.

 namespace Editor3.Modules.ViewModels { public class CreateViewModel : ViewModelBase { public CreateViewModel(World World) { this.World = World; CreateCommand = new Command(Create); } public Command CreateCommand { get; private set; } [Model] World World { get { return GetValue<World>(WorldProperty); } set { SetValue(WorldProperty, value);} } [ViewModelToModel("World")] public string Description { get { return GetValue<string>(DescriptionProperty); } set { SetValue(DescriptionProperty, value); } } [ViewModelToModel("World")] public string Name { get { return GetValue<string>(NameProperty); } set { SetValue(NameProperty, value); } } public static readonly PropertyData WorldProperty = RegisterProperty("World", typeof(World)); public static readonly PropertyData DescriptionProperty = RegisterProperty("Description", typeof(string), null); public static readonly PropertyData NameProperty = RegisterProperty("Name", typeof(string), null); } } 
  • Thank you very much! He killed 6 hours to find an answer, but it turned out he was digging in the wrong direction! In my case, everything worked. As for the documentation on the tutor, I will surely read again so as not to kill so much time for not that! - Artem Hohryakov