简体   繁体   English

设置组合框的selectedValue

[英]set selectedValue of combobox

I have a ComboBox 'cbSkillLevel' to which I am binding data as shown bellow to populate the list. 我有一个ComboBox'cbSkillLevel',我将数据绑定到其中,如下所示以填充列表。 It works fine. 工作正常。 I have a list box with items, based on the items selection I need to set different value to the 'cbSkillLevel'. 我有一个带有物品的列表框,根据物品选择,我需要为“ cbSkillLevel”设置不同的值。 How do I set the value of combobox to the value I want. 如何将组合框的值设置为所需的值。

What I tried is bellow. 我尝试的是波纹管。

Your help much appreciated. 非常感谢您的帮助。

 <ComboBox Grid.Column="3" x:Name="cbSkillLevel" Margin="0,0,10,0" IsEnabled="{Binding ElementName=SkillsPage, Path=setIsEnabled, Mode=OneWay}">
     <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock FontSize="26" Foreground="Black" Text="{Binding Value}" />
        </DataTemplate>
     </ComboBox.ItemTemplate>
 </ComboBox>

List<Lookup> skillLevel = dbOperation.getLookupData("Skill_Level");
cbSkillLevel.ItemsSource = skillLevel;


private void lbSkills_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   clearFields();
   clearTopicsForm();
   var skillobj = (lbSkills.SelectedItem as Skill);

   if (skillobj != null)  
   { 
       selected_SkillDescription = skillobj.SkillDescription;
       tbNotes.Text = skillobj.Notes;
       tbDescription.Text = selected_SkillDescription;
       /*I TRIED THE BELLOW OPTION BUT DID NOT WORK.*/
       //cbSkillLevel.SelectedItem = skillobj.SkillLevel;
       //cbSkillLevel.SelectedIndex = cbSkillLevel.Items.IndexOf(skillobj.SkillLevel.ToString());                
       //cbSkillLevel.SelectedIndex = 1;
       //cbSkillLevel.ItemsSource = tempListSkillLevel;
       cbSkillLevel.SelectedValue = skillobj.SkillLevel.ToString();
       cbSkillType.SelectedItem = skillobj.SkillType;
       tbWarningWeeks.Text = skillobj.WarningWeeks.ToString();
       dpValidFrom.Date = skillobj.ValidFrom;
       dpValidUntil.Date = skillobj.ValidUntil;
       /*load topics for this skill*/
       loadTopicsFromDB(skillobj.SkillDescription);
       //set the fields as read only
       setIsEnabled = false;
       addEditButton();            
     }
  }

I though using 'cbSkillLevel.SelectedValue = skillobj.SkillLevel.ToString();' 我虽然使用了'cbSkillLevel.SelectedValue = skillobj.SkillLevel.ToString();' should work, but it does not. 应该可以,但是不能。 My guess is cuz the binding is set to the value 'Value' 我的猜测是因为绑定设置为值'Value'

<TextBlock FontSize="26" Foreground="Black" Text="{Binding Value}" />

And while setting a value I am assigning from Skill object. 在设置值时,我是从Skill对象分配的。 How can I set value 'cbSkillLevel.SelectedValue = skillobj.SkillLevel.ToString();' 如何设置值“ cbSkillLevel.SelectedValue = skillobj.SkillLevel.ToString();”

class Skill 
{
    [SQLite.PrimaryKey,   SQLite.AutoIncrement]
    public int Id { get; set; }
    public string SkillDescription { get; set; }
    public string SkillType { get; set; }
    public string SkillLevel { get; set; }
    public DateTime ValidFrom { get; set; }
    public DateTime ValidUntil { get; set; }
    public int WarningWeeks { get; set; }
    public string Notes { get; set; }
}

class Lookup
{
    [SQLite.PrimaryKey, SQLite.AutoIncrement]
    public int LookupId { get; set; }
    public string Type { get; set; }
    public string Value { get; set; }
}

SelectedItem probobly don't work since the objects are differnt physcials objects, created in seperate lists. SelectedItem可能不起作用,因为这些对象是在单独的列表中创建的不同的物理对象。 That you used {Binding Value} isn't the problem, that is only the visual representation of your objects and have nothing to do with the selection. 使用{Binding Value}并不是问题,这只是对象的视觉表示,与选择无关。 The thing you need to do is to tell the combobox WHAT value that is the "SelectedValue" on that object. 您需要做的就是告诉组合框该对象上的“ SelectedValue”值为什么。 that is done in the XAML and suggestly to Value since that seams to be unice to your objects(?) 这是在XAML中完成的,建议使用Value,因为这种接缝对您的对象是统一的(?)

<ComboBox SelectedValuePath="Value" ...

Then this should work: 然后这应该工作:

cbSkillLevel.SelectedValue = skillobj.SkillLevel.Value;

ItemSource is a List of Lookup, so the only way to set the "selected" must also be of Lookup type. ItemSource是查找列表,因此设置“选定”的唯一方法也必须是查找类型。

cbSkillLevel.SelectedValue = skillobj.SkillLevel.ToString();

This will only work if you already have established an ItemSource with 仅当您已经使用

IEnumerable<string>

ComoboBox is an ItemsControl. ComoboBox是一个ItemsControl。 You can see a detailed info here https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol(v=vs.110).aspx 您可以在这里查看详细信息https://msdn.microsoft.com/zh-cn/library/system.windows.controls.itemscontrol(v=vs.110).aspx

Add DisplayMemberPath and SelectedValuePath to your xaml bindings that is DisplayMemberPath is what property of object should be shown to the user and SelectedValuePath is what property it selects internally so its like this for example if i want to bind my fruits collection to combobox, i would like display fruit name but select its id so my properties will be 将DisplayMemberPath和SelectedValuePath添加到您的xaml绑定中,即DisplayMemberPath是应该向用户显示对象的属性,而SelectedValuePath是它在内部选择的属性,因此,例如,如果我要将我的水果集合绑定到组合框,我想显示水果名称,但选择其ID,以便我的属性为

 DisplayMemberPath = "FruitName"
 SelectedValuePath = "FruitId"

And later in your selectionchanged event, you can set its value with cbSkillLevel.SelectedValue. 然后在您的selectionchanged事件中,可以使用cbSkillLevel.SelectedValue设置其值。 In our example, it will be the fruit id as we bound it as SelectedValuePath. 在我们的示例中,它将是水果ID,因为我们将其绑定为SelectedValuePath。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM