【问题标题】:c# wpf multiple dictionaries to datagridc# wpf 多个字典到datagrid
【发布时间】:2017-02-13 09:56:59
【问题描述】:

我有多个字典。它们中的每一个都包含相同的键,只是具有不同的值。我正在使用它们进行翻译。它们是动态创建的..

所以,它看起来像这样:

词典英语: first_page_name = "第一页" second_page_name = "第二页"

DictionaryRu: first_page_name = "Первая страница" second_page_name = "Вторая страница"

我希望这些信息显示在 DataGrid 中,或者用户可以更改这些值。它应该看起来像this,最终值是可编辑的。

这样做的最佳做法是什么?

【问题讨论】:

  • 你了解 MVVM 吗?如果我给你看一个假设你有一个 ViewModelBase 的例子,你可以吗?
  • 问题是 Dictionary 中的对象是使用 JSON 反序列化器动态创建的...所以,我不知道 Key 或 Value 值将是什么...或者,有没有办法使用 MVVM实现这一目标?
  • 好的,我将使用在 vm c.tor 中调用的存根函数来模拟字典的创建

标签: c# wpf listview dictionary datagrid


【解决方案1】:

基本上你需要将 DataGrid 绑定到一个集合

    <DataGrid 
        ItemsSource="{Binding Lines}" AutoGenerateColumns="True"

我将使用 ViewModel c.tor 中调用的存根函数来模拟字典的创建

public class MyVM : ViewModelBase
{
    public MyVM()
    {
        Line.DictionaryEng = Line.DictionaryEngStub();
        Line.DictionaryRu = Line.DictionaryRuStub();
        lines = new ObservableCollection<Line>(Line.DictionaryEng.Keys.Select(k => new Line() { KeyWord = k }));
    }
    private ObservableCollection<Line> lines;
    public ObservableCollection<Line> Lines
    {
        get { return lines;  }
        set
        {
            lines = value;
            OnPropertyChanged("Lines");
        }
    }
}

其中底层类定义如下

public class Line : ViewModelBase
{
    internal static Dictionary<string, string> DictionaryEngStub()
    {
        return new Dictionary<string, string>()
        {
            { "first_page_name ","First page" },
            { "second_page_name  ","Second page" }
        };
    }
    internal static Dictionary<string, string> DictionaryRuStub()
    {
        return new Dictionary<string, string>()
        {
            {"first_page_name ","Первая страница" },
            {"second_page_name  ","Вторая страница" }
        };
    }
    internal static Dictionary<string, string> DictionaryEng = new Dictionary<string, string>();
    internal static Dictionary<string, string> DictionaryRu = new Dictionary<string, string>();
    private string keyWord; 
    public string KeyWord
    {
        get { return keyWord;  }
        set
        {
            keyWord = value;
            OnPropertyChanged("keyWord");
        }
    }
    public string EnglishWord {
       get
        {
            string english;
            if (DictionaryEng.TryGetValue(keyWord ?? "", out english))
            {
                return english;
            }
            return null;
        }
    }
    public string RussianhWord
    {
        get
        {
            string russian;
            if (DictionaryRu.TryGetValue(keyWord ?? "", out russian))
            {
                return russian;
            }
            return null;
        }
    }
}

请注意,翻译只有一个 getter 来从字典中检索字符串。 您可以通过添加一个 setter 将新翻译保存到持久层中轻松地使它们可编辑。此外,英语和俄语词典足够通用,可以重命名为 from/to 词典。一旦用户在另一个组合框中选择了一种语言,您就可以相应地重置字典。 由于我这里有一个存根,所以setter没有多大意义,只是为了给你和想法......

    private string englishSaved;
    public string EnglishWord {
       get
        {
            if (englishSaved != null)
            {
                return englishSaved;
            }
            string english;
            if (DictionaryEng.TryGetValue(keyWord ?? "", out english))
            {
                return english;
            }
            return null;
        }
        set
        {
            englishSaved = value; //save the new translation into a persistence layer
        }
    }

【讨论】:

  • 如果只有吸气剂,用户将如何更改值?还有,我也不知道用户会选择什么语言……所以,他可以选择英语、俄语、塞尔维亚语……
  • 正如我所写的,您只需将 setter 添加到要保存新值的位置。并且很容易使用用户选择的语言绑定添加另一个组合框。在这一点上,你的问题是模糊和不完整的
猜你喜欢
  • 2011-03-27
  • 2021-07-04
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
  • 2021-12-22
  • 2018-05-16
相关资源
最近更新 更多