Md. Abdul Alim
Md. Abdul Alim

Reputation: 705

WPF: DataTemplate and UserControl map not working

I'm starting a WPF project. Trying to binding usercontrol by viewmodel. Where viewmodel define with dataType at DataTemplate in Application.Reousrces. But user control not bind. Any one can help me?

    <Application.Resources>
        <DataTemplate DataType="{x:Type vm:MatterPanelViewModel}">
            <uc:MatterPanel />
        </DataTemplate>
    </Application.Resources>

Main Window where will bind user control.

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:uc="clr-namespace:MyProject"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" x:Class="MyProject.MainWindow"
        Title="MyProject" WindowState="Maximized" d:DataContext="{d:DesignInstance Type=uc:MainWindowViewModel}">
    <Grid Grid.Row="2">
         <ContentControl Content="{Binding CurrentViewModel}" Margin="10,0,10,10" />
    </Grid>
</Window>

CurrentViewModel is the property of MainViewModel.

public class MainWindowViewModel:ViewModelBase
{
 private ViewModelBase _currentViewModel;
        public ViewModelBase CurrentViewModel
        {
            get { return this._currentViewModel; }
            set
            {
                if(this._currentViewModel == value) { return; }
                this._currentViewModel = value;
                this.NotifyOfPropertyChange(() => this.CurrentViewModel);
            }
        }

    public MatterPanelViewModel MatterPanelViewModel { get; set; }
public MainWindowViewModel()
        {
            this.MatterPanelViewModel = ServiceLocator.Current.GetService<MatterPanelViewModel>();
        }
}



 public class MatterPanelViewModel:ViewModelBase
        {
            public MatterPanelViewModel()
            {

            }
        }

ViewModelBase here,

public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged
        {
            add { this._propertyChanged += value; }
            remove { this._propertyChanged -= value; }
        }

        private event PropertyChangedEventHandler _propertyChanged = delegate{ };

        protected void NotifyOfPropertyChange<T>(Expression<Func<T>> property)
        {
            var lambda = (LambdaExpression)property;
            MemberExpression memberExpression;
            if(lambda.Body is UnaryExpression)
            {
                var unaryExpression = (UnaryExpression)lambda.Body;
                memberExpression = (MemberExpression)unaryExpression.Operand;
            }
            else
            {
                memberExpression = (MemberExpression)lambda.Body;
            }
            this.NotifyOfPropertyChange(memberExpression.Member.Name);
        }
        public void NotifyOfPropertyChange(string property)
        {
            this.RaisePropertyChanged(property, true);
        }
        private void RaisePropertyChanged(string property, bool verifyProperty)
        {
            var handler = this._propertyChanged;
            if(handler != null)
            {
                handler(this, new PropertyChangedEventArgs(property));
            }
        }
    }

Upvotes: 1

Views: 444

Answers (1)

Md. Abdul Alim
Md. Abdul Alim

Reputation: 705

Finally I solve the problem. The viewmodel and usercontrol map should under the MainWindow but here under main app. I just code from main app

<Application.Resources>
        <DataTemplate DataType="{x:Type vm:MatterPanelViewModel}">
            <uc:MatterPanel />
        </DataTemplate>
    </Application.Resources>

to Main Window

<Window.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
        <DataTemplate DataType="{x:Type vm:MatterPanelViewModel}">
            <usc:MatterPanel/>
        </DataTemplate>
    </Window.Resources>

Then its working well.

Upvotes: 1

Related Questions