2

Hi i am trying to display usercontrol Dynamically But it is not working ...please help me to improve code . In cunstructor of MainWindowViewModel i tried to set initial property of contentcontrol. Thank you in advance

<Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:VM="clr-namespace:WpfApplication1.ViewModel"
            xmlns:View="clr-namespace:WpfApplication1.View"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <DataTemplate DataType="{x:Type VM:FirstControlViewModel}">
                <View:FirstControl></View:FirstControl>
            </DataTemplate>
            <DataTemplate DataType="{x:Type VM:SecondControlViewModel}">
                <View:SecondControl></View:SecondControl>
            </DataTemplate>

        </Window.Resources>
        <Grid>
            <ContentControl Content="{Binding LoadedControl}" />
        </Grid>
    </Window>

View Model Code :-

    public class MainViewModel: INotifyPropertyChanged        
    {
       public MainViewModel()
       {
           LoadedControl = "FirstControlViewModel";// here i am setting property  
                                                   //But not working
       }

       private string _LoadedControl;

       public string LoadedControl
       {
           get { return _LoadedControl; }
           set { _LoadedControl = value;

           NotifyPropertyChanged("LoadedControl");

           }
       }
2
  • Where is the Button? Commented Aug 5, 2015 at 12:20
  • @hossein Actually button command is present on FirstControlViewModel Which is implemented using RealyCommand . But my actual issue is that as i set property in cunstructor it must reflect , Commented Aug 5, 2015 at 12:25

1 Answer 1

4

You need to set LoadedControl to an instance of your ViewModel type, not a string!

public MainViewModel()
{
   LoadedControl = new FirstControlViewModel();
}

private ViewModelBase _LoadedControl;

public ViewModelBase LoadedControl
{
    get { return _LoadedControl; }
    set { _LoadedControl = value;
          NotifyPropertyChanged("LoadedControl");
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you It works but I have one more issue you notice in My XAML file
What is the issue in your XAML file?
Thank you It works but I have one more issue you notice in My XAML fille if i make it ViewModelBase it is not moeking for SecondUserControl view mode
FirstControlViewModel and SecondControlViewModel both must inherit from ViewModelBase. Your XAML looks fine. You must say what the problem is...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.