0

I have two windows act as CRUD, both have the same ComboBox items:

<ComboBox x:Name="LessonTypeNameText" materialDesign:HintAssist.Hint="نوع الدرس و الموضوع"  Style="{StaticResource MaterialDesignFloatingHintComboBox}" >
     <ComboBoxItem>السنة الأولى: من هدي القرآن الكريم  == من دلائل القدرة ( الأنعام: 95 ـ 99).</ComboBoxItem>
     <ComboBoxItem>السنة الأولى: من هدي القرآن الكريم == الوصايا العشر ( الأنعام: 151 ـ 152).</ComboBoxItem>
     <ComboBoxItem>السنة الأولى: من هدي القرآن الكريم == صفات عباد الرحمن ( الفرقان: 63 ـ 77).</ComboBoxItem>
     <ComboBoxItem>السنة الأولى: من هدي القرآن الكريم == الوصايا العشر ( الأنعام: 151 ـ 152).</ComboBoxItem>
</ComboBox>

Now if data pulled from database to fill the first edit window, the ComboBox select the index based in the value from the database, but the second window it's ComboBox doesn't select the index. After some digging I found the comparison is fail in the second window, I tried to see if there a hidden character but there isn't.

private void KafaaOstadiaWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        try
        {
            _reportDetails = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(_KafaaReport[1]);
        }
        catch (Newtonsoft.Json.JsonException ee)
        {
            MessageBox.Show(ee.ToString());
        }
        //MessageBox.Show(_reportDetails["teacherId"]);
        ComboBoxItem comboBoxItem = null;

        comboBoxItem = sifaTeacher01ComboBox.Items.OfType<ComboBoxItem>().FirstOrDefault(x => x.Content.ToString() == _reportDetails["sifaTeacher01"].ToString());
        sifaTeacher01ComboBox.SelectedIndex = sifaTeacher01ComboBox.Items.IndexOf(comboBoxItem);

        schoolTeacher01Text.Text = _reportDetails["schoolTeacher02"];

        //MessageBox.Show(_reportDetails["LessonTypeName01"].ToString());

        comboBoxItem = LessonTypeNameText.Items.OfType<ComboBoxItem>().FirstOrDefault(x => x.Content.ToString() == _reportDetails["LessonTypeName01"].ToString());
        LessonTypeNameText.SelectedIndex = LessonTypeNameText.Items.IndexOf(comboBoxItem);
    }

This is the value from the database:

السنة الأولى: من هدي القرآن الكريم == صفات عباد الرحمن ( الفرقان: 63 ـ 77).

The index 2 should be selected but not, all other combo's work fine. What I am missing here?

UPDATE:

If the content is an English or Latin then the ComboBoxItem get selected, but if I return it back to Arabic then it does not found.

H Aßdøµ
  • 2,925
  • 4
  • 26
  • 37
  • This doesn't feel right, those items shouldn't be hardcoded, they should have their table on the database and joined by their ids, that case you will avoid any encoding-related problem! – SamTh3D3v Mar 01 '18 at 01:21
  • 1
    It should be hardcoded in some case, like gender. – Red Wei Mar 01 '18 at 04:17
  • Please have a look [here]( https://stackoverflow.com/questions/48008220/wpf-combobox-selected-item-with-reference-to-an-object/48008314#48008314). I'd like to add: 2 strings having the same content are 2 different objects. – gomi42 Mar 01 '18 at 10:18
  • @gomi42 I am really lost here, so is there away to compare by content? Also, why it does work in the second window but it fails in the second window? – H Aßdøµ Mar 01 '18 at 22:42

1 Answers1

0

As mentioned by Usama your approach doesn't feel right. Because exactly this approach creates your problem. In case you want to keep your basic idea do the following:

1: create a list of your 4 strings (combobox items ) in code

2: assign this list to the Items property of the combobox

Now you can do the string comparison based on the list on your own and set the SelecetIndex (instead of SeletectItem) property.

gomi42
  • 2,449
  • 1
  • 14
  • 9