在 ListView 中显示绑定组合框

Display Binding ComboBox Inside ListView

我有一个JSON,如下图:

我在 ListView 中有一个 ComboBox。我想在 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

代码:

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;
}

对类:

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;
        }
    }
}

从上面的代码来看,我没有成功在ListView中显示成ComboBox,导致ComboBox为空,如下图: 如何将其显示到组合框中?

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:

如果您想访问 DataType 之外的 属性,请使用 Binding ElementName=Control Name 然后从父 DataContext 访问外部 属性 .请注意,您需要将当前页面 DataContext 设为 this.DataContext = this;。它可以确保您可以从 DataTemplate.

后面的代码中访问 PilihanS
<ComboBox 
x:Name="pairOption"    
ItemsSource="{Binding DataContext.PilihanS, ElementName=ListPairOption}"
PlaceholderText="Pilih" />

代码隐藏

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

并删除上面代码中的这一行 List<ComboBoxClass> PilihanS = new List<ComboBoxClass>();