WPF / MVVM Design Suggestions

I am new to WPF and I am looking for some recommendations here.

I am working on an application that will be used to print work orders for our execution department.

Now I have 2 windows: the first is the main screen, the second is a window with a gridview that will hold work orders for printing. A.

There will be several buttons on the first page. Each button will open a second window; however, depending on which button is pressed, the parameters passed to the service that will load the data will be different.

What would be the best way to do this?

  • Is there a way to define these parameters somewhere on the Button control and then pass them through ICommand / RelayCommand?
  • Should I create a UserControl / ServerControl that will allow me to create additional properties?
  • Anything else I don't think about?

Edit:
To give a rough example (and this is very simplified), let's say I have two sets of criteria: OrderTypes: {Rush, Today, Future} and locations {Warehouse 1, Warehouse 2, Warehouse 3}

The main window will have a 3x3 grid of buttons, one for each combination. I would like to be able to specify one button "Expedite and Warehouse 1"; and then pass these parameters back to one method, which will open a second window.

+4
source share
4 answers

Suppose you have MainWindow, and buttons are placed in it.

  • Create MainWindowViewModeland install it as DataContextfor MainWindow.

  • ICommand ViewModel bind ICommand, . ICommand RelayCommand, DelegateCommand , .

  • , , . , Enum , .

Enum

public enum ActionType
{
   Action1,
   Action2,
   Action3 and so on...
}

:

<Button Command="{Binding CommandInstance}"
        CommandParameter="{x:Type local:ActionType.Action1}"/>

<Button Command="{Binding CommandInstance}"
        CommandParameter="{x:Type local:ActionType.Action2}"/>

local , enum.

enum :

private void CommandMethod(ActionType action)
{
    AnotherWindow anotherWindow = new AnotherWindow(action);
    anotherWindow.Show();
}

, , , , .

, ViewModel , Service , / .


UPDATE

Views, . , IMultiValueConverter.

:

<Button Command="{Binding TestCommand}">
   <Button.Resources>
      <sys:String x:Key="Rush">Rush</sys:String>
      <sys:String x:Key="Warehouse">Warehouse</sys:String>
    </Button.Resources>
    <Button.CommandParameter>
       <MultiBinding Converter="{StaticResource MultiValuesReturnConverter}">
          <Binding Source="{StaticResource Rush}"/>
          <Binding Source="{StaticResource Warehouse}"/>
        </MultiBinding>
    </Button.CommandParameter>
 </Button>

sys System XAML:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

, XAML, XAML . , , "" .

, , :

public class MultiValuesReturnConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType,
                          object parameter, CultureInfo culture)
    {
        return values.ToList<object>();   
    }

    public object[] ConvertBack(object value, Type[] targetTypes,
                                object parameter, CultureInfo culture)
    {
        throw new System.NotImplementedException();
    }
}

:

private void CommandMethod(object parameter)
{
    // Now you have all the parameters here to take decision.
    IList<object> values = parameter as List<object>;

    AnotherWindow anotherWindow = new AnotherWindow(action);
    anotherWindow.Show();
}
+3

- , click . , .

MVVM " ". , -, , " ".

, , .

+1

, MVVMLight, , Messenger, RelayCommands ..

  • Tag, , , (), CommandParameter :

: , . ( FrameworkElement.)

CommandParameter:

<Button Content="Parameterized Command" 
    Command="{Binding ParameterizedCommand}" CommandParameter={Binding SomeObject} /> 
  • , UserControl, .
  • Messenger ViewModel ( , MVVM).
  • MVVMLight , ViewModels.
  • MVVMLight , .
  • , UIElements, ButtonBase , Click, CommandParameters UIElements , EventToCommand, MVVMLight , .

, , .

0

( INotifyPropertyChanged DependencyProperty):

, DataContext OrderViewModel MainWindowViewModel

class MainWindowViewModel : ViewModelBase // ViewModelBase should implement INotifyPropertychanged, unless you're using dependency properties
{
  private OrderViewModel _OrderViewModelInstance;
  public OrderViewModel OrderViewModelInstance 
  { 
     get{ return _OrderViewModel;} 
     set { _OrderViewModel = value; 
          OnPropertyChanged("OrderViewModel")} // Method from ViewModelBase
   }

, :

  • OrderViewModel MainWindowViewModel (, ) .
  • DataContext OrderViewModelInstance XAML. , , , .
0

Source: https://habr.com/ru/post/1526176/


All Articles