0

I need to create a small GUI application to manually invoke some driver commands. In total, there are about 40 commands and some of them with additional arguments.

The request is, that all commands should be on a single page. Apart from this, there are no requirements and since it is just a testing environment, the user experience is only a minor concern.

When I start doing it the "regular" way and just add buttons and input elements for the arguments, I end up with a really cluttered UI. It also feels plain wrong to do something like

<Button Command="{Binding DriverCommand} CommandParameter=1/> 

For all 40 commands

So my question is, what would be a good way to make lots of commands with varying parameters available? I would prefer an MVVM way, simply because I want to further learn this pattern. An implementation where I have my commands (as enums?) in my viewmodel and can just bind it to a control in the view, would be ideal. I could then reuse the UI with different drivers (which is something that is very probable).

Thanks in advance

4

1 に答える 1

2

次のようなものを使用できます。

ビューモデル:

public class CommandDashboardViewModel
{
    public ObservableCollection<DriverCommandViewModel> DriverCommands { get; set; }
}

public class DriverCommandViewModel
{
    // all of these properties have to implement INPC
    public string CommandText { get; set; }
    public ICommand Command { get; set; }
    public object CommandParameter { get; set; }
}

意見:

<!-- the DataContext of this view is instance of CommandDashboardViewModel -->
<ItemsControl ItemsSource="{Binding DriverCommands}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Content={Binding CommandText} Command={Binding Command} CommandParameter={Binding CommandParameter}/> 
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

頭のてっぺんにまとめたので、構文エラーについてはお詫びします。

于 2012-04-30T09:55:24.233 に答える