控制添加键/值对? [英] Control for adding key/value pairs?

查看:29
本文介绍了控制添加键/值对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个控件来允许用户添加/删除/编辑键/值对的行.用于此目的的最佳控件是什么?有人有例子吗?我对 WPF 很陌生.

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.

推荐答案

我会使用带有两个标签文本框的简单对话,新的一对将被添加到应该绑定到 DataGrid 的原始数据中 以便自动生成行.

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.

DataGrid 示例解决方案.
XAML:

A DataGrid example solution.
XAML:

<Window
    ...
    DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
    <StackPanel Orientation="Vertical">
        <DataGrid ItemsSource="{Binding GridData}"/>
    </StackPanel>
</Window>

背后的代码:
在窗口类中:

Code behind:
In window class:

    private ObservableCollection<Pair> gridData = new ObservableCollection<Pair>(new Pair[]{new Pair()});
    public ObservableCollection<Pair> GridData
    {
        get { return gridData; }
    }

配对类:

public class Pair : INotifyPropertyChanged
{
    private string key = "Key";
    public string Key
    {
        get { return key; }
        set
        {
            if (this.key != value)
            {
                key = value;
                NotifyPropertyChanged("Key");
            }
        }
    }


    private double value = 0;
    public double Value
    {
        get { return value; }
        set
        {
            if (this.value != value)
            {
                this.value = value;
                NotifyPropertyChanged("Value");
            }
        }
    }

    public Pair() { }
    public Pair(string key, double value)
        : this()
    {
        Key = key;
        Value = value;
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

您应该能够使用普通的 DataGrid 功能添加新对.

You should be able to add new pairs with the normal DataGrid functionality.

这篇关于控制添加键/值对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆