Addition of teams to UserControl wpf



  • Got it. UserControl♪ I want to use it in two projects. There's this idea: UserControl Put the entire functionality that can be accessible, and in the annex, just connect to the interface elements. As I see it, I must have to use the teams. Can you tell me how to do this? Examples of what I found online tend to look at the existing WPF teams.

    UPD:Got it. UserControl on which ListBox to display the list of vehicles from the OBD system. That's it. UserControl I'd like to add the additive/removal teams. The teams will carry out the stored procedure from the OBD system. On the main form of the application, I put the buttons and mine. UserControl♪ To the buttons, I want to tie the team up. UserControl♪ That is, in the very annex, I'm no longer writing on the procedures stored, that's all already in place. UserContorol



  • Let's try the example.

    public partial class MyUserControl : UserControl
    {
        ...
    
    public ICommand MyCommand
    {
        get { return (ICommand)GetValue(MyCommandProperty); }
        set { SetValue(MyCommandProperty, value); }
    }
    
    public static readonly DependencyProperty MyCommandProperty =
            DependencyProperty.Register("MyCommand", typeof(ICommand), typeof(MyUserControl));
    
    ...
    

    }

    That's where we're in control of the team, and what we described as her. DependencyProperty Give us the opportunity to bind her team. ViewModellike that.

    Now at the right moment, we need to launch it. Let this moment be the event of the shipment of the counter. Add the code above

    private void MyUserControl_Loaded(object sender, RoutedEventArgs e)
    {
    MyCommand?.Execute(/Тут указывается параметр для команды, можно null/);
    }

    We're done on the counter. Now we'll tie our team to the real action. ♪ ViewModel We'll describe the team that will just show MessageBox♪ Implementation ICommand I use it most often. RelayCommand of MvvmLight

    public RelayCommand ShowMessageCommand { get; private set; }

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

    And the designer. ViewModel initial

    ShowMessageCommand = new RelayCommand(ShowMessage);

    Now, like in the window, as DataContext of which a copy of our ViewModel Describe the following:

    <local:MyUserControl
    ...
    MyCommand={Binding Path=ShowMessageCommand}/>

    Now when you call the line. MyCommand?.Execute performance ShowMessage♪ In order to complete the painting, the parameters are not sufficient. But there's nothing complicated. If you don't do it yourself, ask.

    UPDATE

    To tie the button to the counter MyCommand♪ Just a Bindim ♪ Button.Command to MyCommand♪ That way, a team will be performed on the button. Do it like that.

    Т.е. в самом приложении, я уже не прописываю выполнение хранимых процедур, это все уже есть в UserContorol

    Absolutely wrong. Controls should not take this function.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2