【问题标题】:Display Binding ComboBox Inside ListView在 ListView 内显示绑定 ComboBox
【发布时间】:2022-01-03 09:55:02
【问题描述】:

我有一个 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为空,如下图: 如何将其显示到 ComboBox 中?

【问题讨论】:

    标签: c# json listview uwp combobox


    【解决方案1】:

    从上面的代码来看,我没有成功显示到ListView中的ComboBox中,导致ComboBox为空,如下图:

    如果你想访问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&lt;ComboBoxClass&gt; PilihanS = new List&lt;ComboBoxClass&gt;();上方的代码中删除这一行

    【讨论】:

    • 我试过了,但是 ComboBox 还是空的
    • 请确保 PilihanS 是这样的公共财产。 public List&lt;ComboBoxClass&gt; PilihanS { get; set;} = new List&lt;ComboBoxClass&gt;();
    猜你喜欢
    • 2019-12-27
    • 2013-09-19
    • 1970-01-01
    • 2018-08-02
    • 2023-03-02
    • 2018-06-01
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多