Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

UserControl with command

$
0
0

Hi there again. i have a custom user control which is dynamically added in my main view.
inside my usercontrol there's a button, i want that button to execute a method located in my mainVM, before posting this question, i have tried many solutions. but all of them doesn't work considering that i almost have the same code.I think is something problem or much more deeper understanding with WPF. please check my code below.

Usercontrol.xaml

<MyControl ....>
 ...<Button  Command="{Binding SomeCommand}">MyButton</Button>
 ...</MyControl>

Usercontrol.xaml.cs

public partial class MyControl: UserControl{
        public MyControl() {
            InitializeComponent();
        }

        public ICommand SomeCommand {
            get { return (ICommand)GetValue(SomeCommandProperty); }
            set { SetValue(SomeCommandProperty, value); }
        }

        public static readonly DependencyProperty SomeCommandProperty =
            DependencyProperty.Register("SomeCommand", typeof(ICommand), typeof(MyControl), new UIPropertyMetadata(null));
....
}


MainView.xaml

...<!-- then the uc is added dynamically--><ListBox..><ListBox.ItemTemplate><DataTemplate><uc:MyControl SomeCommand="{Binding CommandFromMainVm}"></DataTemplate></ListBox.ItemTemplate></uc:IdleEntryView></ListBox> ...

MainVM.cs

....

 private DelegateCommand commandFromMainVm;

public ICommand CommandFromMainVm{
     get {
          if (commandFromMainVm== null) {
                    commandFromMainVm= new DelegateCommand(ExecuteThisMethod);
         }
                return commandFromMainVm;
         }
}

private void ExecuteThisMethod(){
//i have some logics here and i need to execute it
//trough my user control button click.
}

btw. im using MVVM light toolkit from code flex.

Thanks in advance.

fadz


 




Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>