I am getting an array of data from xml, I want to rename certain values. For the Combobox, this is implemented as follows:

private void ComboBox_Loaded(object sender, RoutedEventArgs e) { cbNews.ItemsSource = _newsKeys; cbNews.SelectedIndex = 0; } 

_newsKeys:

 _newsKeys = new Dictionary<string, string>() { { AllRealms, "All News" }, { "realm1", Settings.Default.server_name_wotlk }, { "realm2", Settings.Default.server_name_cata }, { "realm3", Settings.Default.server_name_mop }, { "realm4", Settings.Default.server_name_wod }, }; 

I do not understand how to rename this data in the listbox, which has Textblock.Text = "{Binding Realms}"

  • It’s not quite clear why you want to rename something. For different cases there are different approaches. Describe in more detail why you need it. - VladD
  • @VladD, in more detail, how much you can, I can show in the chat or Skype, if you do not mind. Most likely I do not know how to ask questions ( - Elizabeth
  • News = newsSet.ExpressNews; listbox.ItemsSource = newsSet.ExpressNews; News contains an array of xml data that went through deserialization and parsed the objects in variables in xml. In the listbox itself there is a DataTemplate in which there is a textblock that receives one of the variables (Realms), the received data from xml is also connected to the combobox - cbNews.ItemsSource = _newsKeys ;. I want to do the same, so that the data that the combobox receives and renames to values ​​does the same for the listbox - Elizaveta
  • Okay, this is more interesting. Well, if your data changes in one control, and the other does not automatically pick up, then apparently, your data type, which is a list item, does not implement INotifyPropertyChanged . - VladD
  • I will try to create a chat, if possible. - VladD

2 answers 2

In my case, it was decided in this way.

 public class ExpressNewsItemViewModel : ExpressNewsItem { public ExpressNewsItemViewModel(ExpressNewsItem item, IDictionary<string, string> correlations) { Item = new ExpressNewsItem(); if (correlations != null && !string.IsNullOrEmpty(item.Realms) && correlations.ContainsKey(item.Realms)) { RealmsFriendly = correlations[item.Realms]; } else { RealmsFriendly = item.Realms; } } public string RealmsFriendly { get; protected set; } public ExpressNewsItem Item { get; protected set; } } 

Thanks to everyone who responded!

    Dictionary has methods: ContainsKey() , ContainsValue() , TryGetValue() , properties: Keys , Values , that is, you can always create a collection of keys, or a collection of values ​​and give it to ListBox. You can also search for the desired value in a predefined dictionary by a key that can be compared with the data from your xml.

    • I do not quite understand you, with a previous topic for a very long time I understood ( - Elizaveta
    • I understand from your question that, on the basis of the dictionary, you want to create a list for the ListBox , so I’m telling you where to look. - Bulson
    • Could you give me an approximate version? I try the option, as I did with the combobox, but it does not work like this ( - Elizaveta
    • Well, apparently I do not quite understand what you want to do. Explain what you have at the entrance and what you want to get at the output (with an example at least in a couple of lines.). - Bulson
    • News = newsSet.ExpressNews; листбокс.ItemsSource = newsSet.ExpressNews; News contains an array of xml data that went through deserialization and parsed the objects in variables in xml. In the listbox itself there is a DataTemplate in which there is a textblock that receives one of the variables (Realms), the received data from xml is also connected to the combobox - cbNews.ItemsSource = _newsKeys ;. I want to do the same, so that the data that the combobox receives and renames to values ​​does the same for the listbox - Elizaveta