我在列表视图中有一个网络视图和一个组合框,其数据是从服务器获取的。 我希望组合框的默认值与下面的字符串匹配:
string A = "A:C:B:D:E";
XAML:
<ListView
Name="ListPairOption"
Height="auto">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:PairClass">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid
x:Name="pilganStack"
Grid.Column="0"
Margin="10,10,10,10"
HorizontalAlignment="Stretch"
Background="#FFDFF6EE">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<WebView
x:Name="option"
Margin="5,5,5,5"
MinHeight="60"
Height="auto"
HorizontalAlignment="Stretch"
local:MyProperties.HtmlString="{Binding Name}"
DefaultBackgroundColor="Transparent"/>
<TextBlock
x:Name="index"
FontSize="20"
Text="{Binding Ind}"
Visibility="Collapsed"/>
</Grid>
<ComboBox
x:Name="pairOption"
Grid.Column="1"
Margin="10,10,10,0"
DisplayMemberPath="NameA"
SelectedValuePath="Pilgan"
ItemsSource="{Binding DataContext.PilihanS, ElementName=ListPairOption}"
Background="#FFDFF6EE"
FontSize="17"
FontWeight="SemiBold"
PlaceholderText="Pilih"
Foreground="Black"
PlaceholderForeground="Black"
CornerRadius="10,10,10,10"
SelectionChanged="pairOption_SelectionChanged"
BorderBrush="#FF79D9BD"
BorderThickness="1"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
代码:
private List<MyClass> _items;
string urlPath = "https://";
var httpClient = new HttpClient(new HttpClientHandler());
var response = await httpClient.GetAsync(urlPath);
string jsonText = await response.Content.ReadAsStringAsync();
JsonObject jsonObject = JsonObject.Parse(jsonText);
double id = jsonObject["EXAM_ASM_A_ID"].GetNumber();
string questionID = jsonObject["EXAM_QUESTION_ID"].GetString();
string choiceA = questionObject.ContainsKey("choice_a") && questionObject["choice_a"] != null ? questionObject["choice_a"].GetString() : string.Empty;
string choiceB = questionObject.ContainsKey("choice_b") && questionObject["choice_b"] != null ? questionObject["choice_b"].GetString() : string.Empty;
string choiceC = questionObject.ContainsKey("choice_c") && questionObject["choice_c"] != null ? questionObject["choice_c"].GetString() : string.Empty;
string choiceD = questionObject.ContainsKey("choice_d") && questionObject["choice_d"] != null ? questionObject["choice_d"].GetString() : string.Empty;
string choiceE = questionObject.ContainsKey("choice_e") && questionObject["choice_e"] != null ? questionObject["choice_e"].GetString() : string.Empty;
_items = new List<MyClass>();
_items.Add(new MyClass() { Name = choiceA });
_items.Add(new MyClass() { Name = choiceB });
_items.Add(new MyClass() { Name = choiceC });
_items.Add(new MyClass() { Name = choiceD });
_items.Add(new MyClass() { Name = choiceE });
string c = "";
string v1 = "";
JsonArray mapArray = questionObject["map"].GetArray();
foreach (JsonValue mapValue in mapArray)
{
JsonArray mapArrayI = mapValue.GetArray();
PairClass pair = new PairClass();
foreach (JsonValue mapValueI in mapArrayI)
{
string choice = "";
try
{
if (mapValueI.ToString().All(char.IsDigit))
{
c = String.Concat(mapValueI.ToString().Where(Char.IsDigit));
index = Int16.Parse(c);
pair.Ind = index;
}
else
{
string v = mapValueI.ToString();
var collection = Regex.Matches(v, "\\\"(.*?)\\\"");
foreach (var item in collection)
{
v1 = item.ToString().Trim('"');
choice = v1;
pair.Name = choice;
}
}
}
catch
{
}
}
itemL.Add(pair);
}
JsonArray pairArray = questionObject["pairs"].GetArray();
foreach (JsonValue pairValue in pairArray)
{
string pairKey = "";
string pairString = "";
JsonArray pairArrayI = pairValue.GetArray();
foreach (JsonValue pairValueI in pairArrayI)
{
try
{
string v = pairValueI.ToString();
if (Regex.IsMatch(v, @"\b[A-E]\b"))
{
var collection = Regex.Matches(v, "\\\"(.*?)\\\"");
foreach (var item in collection)
{
string v2 = item.ToString().Trim('"');
pairKey = v2;
}
}
else
{
var collection = Regex.Matches(v, "\\\"(.*?)\\\"");
foreach (var item in collection)
{
string v3 = item.ToString().Trim('"');
pairString = v3;
//Debug.WriteLine("pair string: " + pairString);
if(pairString.Contains("&"))
{
pairString = pairString.Replace("&", " & ");
}
}
}
}
catch
{
}
}
PilihanS.Add(new ComboBoxClass() { Pilgan = pairKey, NameA = pairString });
}
ListPairOption.Visibility = Visibility.Visible;
var list = itemL.OrderBy(p => p.Ind).ToList();
ListPairOption.ItemsSource = list;
public class MyClass
{
public string Name { get; set; }
public override string ToString()
{
return this.Name;
}
}
PairClass.cs
public class PairClass
{
public int Ind { get; set; }
public string Name { get; set; }
public ObservableCollection<ComboBoxClass> PilihanS { get; set; }
public PairClass()
{
Ind = int.MinValue;
Name = string.Empty;
PilihanS = new ObservableCollection<ComboBoxClass>();
}
public PairClass(int ind, string name, string pilihan)
{
Ind = ind;
Name = name;
}
}
public class ComboBoxClass
{
public string Pilgan { get; set; }
public string NameA { get; set; }
public override string ToString()
{
return this.NameA;
}
}
如何设置列表视图中组合框的默认值?
注:
设置默认组合框值
如果您想设置默认的组合框值,您可以尝试将
SelectedIndex
属性绑定到数据上下文的值。当您设置 SelectedIndex
时,组合框将显示源索引处的值。
在您的场景中,
PairClass
是ComboBox
的数据上下文,因此您可以在PairClass
中创建一个新属性,并使用它绑定到ComboBox的SelectedIndex
属性。