Home > Software design >  Display Binding ComboBox Inside ListView
Display Binding ComboBox Inside ListView

Time:11-26

I have a JSON, like the image below: JSON

I have a ComboBox inside a ListView. I want to display "pairs" in the ComboBox.

XAML:

<ListView Name="ListPairOption">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="data:PairClass">
            <StackPanel
                            x:Name="pilganStack">
                                <WebView
                                    x:Name="option"
                                        local:MyProperties.HtmlString="{Binding Name}"/>
                        </StackPanel>
            <ComboBox
                            x:Name="pairOption"
                                DisplayMemberPath="NameA"
                                SelectedValue="{Binding ComboBoxClass, Mode=TwoWay}"
                                ItemsSource="{x:Bind PilihanS}"
                                PlaceholderText="Pilih" />
</ListView

Codes:

try
{   
    JsonObject jsonObject = JsonObject.Parse(jsonText);
        JsonObject questionObject = jsonObject["EXAM_QUESTION"].GetObject();
        ObservableCollection<PairClass> itemL = new ObservableCollection<PairClass>();
        JsonArray mapArray = questionObject["map"].GetArray();
        foreach (JsonValue mapValue in mapArray)
        {
            JsonArray mapArrayI = mapValue.GetArray();
            PairClass pair = new PairClass();
            foreach (JsonValue mapValueI in mapArrayI)
            {
                try
                {
                            string v = mapValueI.ToString();
                                pair.Name = v;
                    }
                }
        }
        itemL.Add(pair);
    }   
    JsonArray pairArray = questionObject["pairs"].GetArray();
    string pairString = "";
        foreach (JsonValue pairValue in pairArray)
        {
                JsonArray pairArrayI = pairValue.GetArray();
                List<ComboBoxClass> PilihanS = new List<ComboBoxClass>();
                foreach (JsonValue pairValueI in pairArrayI)
                {
                    try
                        {
                                    var collection = Regex.Matches(v, "\\\"(.*?)\\\"");
                                        foreach (var item in collection)
                                        {
                                            string v3 = item.ToString().Trim('"');
                                                pairString = v3;
                                 }
                          }
        }
        PilihanS.Add(new ComboBoxClass() { NameA = pairString });
    }
        ListPairOption.ItemsSource = itemL;
}

PairClass:

public class PairClass
    {
        public string Name { get; set; }
        public ObservableCollection<ComboBoxClass> PilihanS { get; set; }

        public PairClass(string name)
        {
            Name = name;
        }
    }

    public class ComboBoxClass
    {
        public string NameA { get; set; }

        public override string ToString()
        {
            return this.NameA;
        }
    }
}

From the code above, I didn't succeed in displaying it into a ComboBox in a ListView so that the ComboBox is empty, as shown below: ComboBox empty How to display it into a ComboBox?

CodePudding user response:

From the code above, I didn't succeed in displaying it into a ComboBox in a ListView so that the ComboBox is empty, as shown below:

If you want to access the property that out of DataType, please using Binding ElementName=Control Name then access the the outside property from the parent DataContext. please note you need to set current page DataContext as this this.DataContext = this;. it could make sure you can access PilihanS where in the code behind from DataTemplate.

<ComboBox 
x:Name="pairOption"    
ItemsSource="{Binding DataContext.PilihanS, ElementName=ListPairOption}"
PlaceholderText="Pilih" />

code behind

public MainPage()
{
    this.InitializeComponent();      
    this.DataContext = this;
}
public List<ComboBoxClass> PilihanS { get; set;} = new List<ComboBoxClass>();

And delete this line where in your code above List<ComboBoxClass> PilihanS = new List<ComboBoxClass>();

  • Related