I need a control to allow users to add/delete/edit rows of key/value pairs. What's the best control to use for this? Does anyone have an example? I'm quite new to WPF.
相关问题
- VNC control for WPF application
- WPF Binding from System.Windows.SystemParameters.P
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
- How to properly change a resource dictionary
I'd use a simple dialogue with two labelled text boxes, the new pair would be added to the original data which should be bound to the
DataGrid
so that rows are generated automatically.Edit: A DataGrid example solution.
XAML:
Code behind:
In window class:
Pair class:
You should be able to add new pairs with the normal DataGrid functionality.
Assuming you're using MVVM, use an ObservableCollection of your pair type (or you could use the System.Collections.Generic.KeyValuePair type if you didn't want to define your own) on your view model.
You can provide the user with two textboxes to add a new pair, which instantiates your pair type and adds it to the ObservableCollection. You can use commands to invoke a method on your view model to do this (or look into a framework such as Caliburn.Micro which supports convention based binding of view controls such as buttons to verbs on your view model).
You could use a ListBox to display the collection of pairs (just bind the ItemsSource property on the ListBox to the view models ObservableCollection of pairs). Alternatively, if you want to support inline editing of existing pairs, then the DataGrid would be a good choice. The DataGrid supports inline editing by double clicking a cell value. You can define a normal cell template and an editing cell template in the DataGrid XAML: