1

Topic: How to make wpf-binding in Grid another ?

I welcome. For simplicity I will formulate the spherical task in vacuum. Let is 2 various wpf-kontrola and one window with , called to display in itself one of . Each form is separately designed on pattern MVVM (it is visible on name spaces more low). Let  look so: <UserControl...> <Grid> <Label Content = "It is my massage1111111111" HorizontalAlignment = "Left" Margin = "77,115,0,0" VerticalAlignment = "Top"/> </Grid> </UserControl> <UserControl...> <Grid> <Label Content = "It is my massage2222222222" HorizontalAlignment = "Left" Margin = "77,115,0,0" VerticalAlignment = "Top"/> </Grid> </UserControl> to Display them in a form window it is possible different methods. One of them such (necessary  ): <Window... xmlns:form1 = "clr-namespace:ClassLibrary1.Form1.ViewControl" xmlns:form2 = "clr-namespace:ClassLibrary1.Form2.ViewControl"> <Grid> <Grid. RowDefinitions> <RowDefinition Height = "Auto"/> </Grid. RowDefinitions> <Grid. ColumnDefinitions> <ColumnDefinition Width = "*"/> </Grid. ColumnDefinitions> <Grid x:Name = "gMyGrid" Grid. Row = "0" Grid. Column = "0"> <! - <form1:ViewControlClass Grid. Row = "0" Grid. Column = "0"/>-> <! - <form2:ViewControlClass Grid. Row = "0" Grid. Column = "0"/>-> </Grid> </Grid> </Window> but me it would be desirable to make it in another way: to transfer these  in not which type of an anchored variable (we name it, for example, MyControl). Somehow approximately so that it looked: <Window...> <Grid> <Grid. RowDefinitions> <RowDefinition Height = "Auto"/> </Grid. RowDefinitions> <Grid. ColumnDefinitions> <ColumnDefinition Width = "*"/> </Grid. ColumnDefinitions> <Grid x:Name = "gMyGrid" Grid. Row = "0" Grid. Column = "0" DataContext = "{Binding Path=MyControl}/> </Grid> </Window> Prompt how correctly to anchor, please, a variable and what at it should be thus type with the code C#? Tried to declare it somehow so in ViewModelClass for the third form (which Window), but this variant does not work: private ClassLibrary1.Form1.ViewModelLib.ViewModelClass myControl; public ClassLibrary1.Form1.ViewModelLib.ViewModelClass MyControl {get {return myControl;} set {gMyGridControl = value; NotifyPropertyChanged ("myControl");}} I Will be grateful for council.

2

Re: How to make wpf-binding in Grid another ?

Hello, lesovick, you wrote: L> But me it would be desirable to make it in another way: to transfer these  in not which type of an anchored variable (we name it, for example, MyControl). Somehow approximately so that it looked: Speak MVVM and words about datatemplate. It like a basic piece and them here needs to be used: ContentControl + binding to Content + datatemplates. L> Prompt how correctly to anchor, please, a variable and what at it should be thus type with the code C#? Tried to declare it somehow so in ViewModelClass for the third form (which Window), but this variant does not work: NotifyPropertyChanged (nameof (MyControl));//capital M, because

3

Re: How to make wpf-binding in Grid another ?

Hello, Sinatr, you wrote: S> Speak MVVM and words about datatemplate. It like a basic piece and them here needs to be used: ContentControl + binding to Content + datatemplates. Saw, how datatemplates use others, at itself did not use. If it helps to anchor correctly a variable, show, please, as it to make. Inscribe, if it is not difficult, a xaml-code in my example as all it should look correct. S> NotifyPropertyChanged (nameof (MyControl));//capital M because  Yes, you are right. I was sealed up, when wrote a question to a forum. In the program a variable from a capital letter all is correct. Only it does not help.

4

Re: How to make wpf-binding in Grid another ?

Hello, lesovick, you wrote: L> saw, how datatemplates use others, at itself did not use. If it helps to anchor correctly a variable, show, please, as it to make. Inscribe, if it is not difficult, a xaml-code in my example as all it should look correct. Create a little ViewModel, specify data template for everyone. If  the property containing the reference to one of such ViewModel, to something that deduces content (ContentPresenter), for example, ContentControl. Content WPF uses data template, transferring the link through DataContext. Xaml: <Window. Resources> <DataTemplate DataType = "{x:Type local:ViewModel1}"> <local:UserControl1/> </DataTemplate> <DataTemplate DataType = "{x:Type local:ViewModel2}"> <local:UserControl2/> </DataTemplate> </Window. Resource>... <ContentControl Content = "{Binding ViewModelInstance}"/>... Cs: public class ViewModelMain: INotifyPropertyChanged {public Base ViewModelInstance {get; set;}//full property with notification or rise notification manually when changing the value} public class Base: INotifyPropertyChanaged {...} public class ViewModel1: Base {...} public class ViewModel2: Base {...}

5

Re: How to make wpf-binding in Grid another ?

Trial and error method I understood a problem. If it is interesting to someone - here the decision. datatemplates did not use. Representation and model-representation look so (the command btnButtonClickCommand causes ChangeControl which changes contents . FormList - the list about 2nd above described ): <Window... <Grid x:Name = "MyGrid"> <Grid. RowDefinitions> <RowDefinition Height = "*"/> <RowDefinition Height = "*"/> </Grid. RowDefinitions> <Grid. ColumnDefinitions> <ColumnDefinition Width = "*"/> </Grid. ColumnDefinitions> <UserControl x:Name = "MyControl1" Content = "{Binding Path=MyControl}" Grid. Row = "0"/> <Button Content = "{Binding Path=ListIndexString}" HorizontalAlignment = "Center" Grid. Row = "1" VerticalAlignment = "Center" Command = "{Binding Path=BtnButtonClickCommand}"/> </Grid> </Window> public class ViewModelClass3: Notifier {private ModelClass3 model; private Int32 listIndex; public String ListIndexString {get {return listIndex. ToString ();} set {listIndex = Int32.Parse (value); NotifyPropertyChanged ("ListIndexString");}} public UserControl MyControl {get {return model. FormList [listIndex];} set {model. FormList [listIndex] = value; NotifyPropertyChanged ("MyControl");}} private ICommand btnButtonClickCommand; public ICommand BtnButtonClickCommand {get {return btnButtonClickCommand;}} public ViewModelClass3 (ModelClass3 _model) {model = _model; listIndex = 0; btnButtonClickCommand = new ButtonClickCommandClass (this);} public void ChangeControl () {if (ListIndexString == "0") ListIndexString = "1"; else ListIndexString = "0"; MyControl = model. FormList [listIndex];}}

6

Re: How to make wpf-binding in Grid another ?

Hello, Sinatr, you wrote: S> Hello, lesovick, you wrote: L>> saw, how datatemplates use others, at itself did not use. If it helps to anchor correctly a variable, show, please, as it to make. Inscribe, if it is not difficult, a xaml-code in my example as all it should look correct. S> create a little ViewModel, specify data template for everyone. If  the property containing the reference to one of such ViewModel, to something that deduces content (ContentPresenter), for example, ContentControl. Content WPF uses data template, transferring the link through DataContext. Thanks big for council, Sinatr. Is mandatory too I will check up.