Usercontrol Binding problem in DetailViewModel

Mar 13, 2012 at 10:12 AM
Edited Mar 13, 2012 at 10:13 AM

Hi,

I have the following ViewModel:

public class SignaletiqueModuleViewModel : ViewModelDetailBase<SignaletiqueModuleViewModel, Agent>
     
         // Default ctor
        public SignaletiqueModuleViewModel(Agent agent)
        {
            this.Model = agent;          
        }

        public DelegateCommand ReadEidCommand
        {
            get
            {
                return new DelegateCommand(ReadEidExecute, ReadEid_CanExecute);
            }            
        }

        private void ReadEidExecute()
        {
            MessageBox.Show("Hello!");
        }

        private bool ReadEid_CanExecute()
        {
            return true;
        }
}

My view is linked to the viewmodel like this:

public partial class SignaletiqueModule : UserControl

    {      
        public SignaletiqueModule(SignaletiqueModuleViewModel viewModel)
        {            
            InitializeComponent();

            this.DataContext = viewModel;   
        }
}

In the Xaml, I have the following code : 
<ToolBarTray Name="ToolBarTrayModule">
            <ToolBar Header="Commandes">
                <gcc:ToolBarButton Command="{Binding ReadEidCommand}" Text="Lecture Eid" ImageSource="{StaticResource Eid_32x32}"/>
            </ToolBar>
</ToolBarTray>
<Button Height="23" HorizontalAlignment="Left" Margin="414,7,0,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding ReadEidCommand}" Content="Lecture Eid" />
The button command is successfully bound to the ReadEidCommand but the userControl does not work. It look like the ReadEidCommand is assigned before the DataContext is set to the ViewModel. The usercontrol contains a button bound to the command via a dependency property.
Any idea?
Thanks a lot!
Mar 13, 2012 at 2:53 PM

Problem solved :-)