c#
  • silverlight
  • combobox
  • 2015-07-10 88 views 0 likes 
    0

    My ComboBoxItems具有表示要选择的项目名称的字符串值,但我已将该项目的值存储在ComboBoxItemTag属性中。我使用Tag作为相当于下拉列表项目Value的asp.net。根据其标记值选择ComboBoxItem

    在后面,我将DataGrid模板列的代码,我设置了ComboBoxItem如下:

    <ComboBoxItem Tag='" + product.ProductGuid + "' Content='" + product.Name + "'></ComboBoxItem> 
    

    我需要根据Tag值不是内容以编程方式选择ComboBoxItem。在下面的代码,currentProduct持有ProductGuid值,我需要选择,但是这个代码将选择其内容的ComboBoxItem是我currentProduct

    ((ComboBox)QuotationDG.Columns[0].GetCellContent(MyData[MyData.Count - 1])).SelectedValue = currentProduct; 
    

    有没有一种方法来设置ComboBox选中的值给ComboBoxItem其标签值是currentProduct

    编辑:

    下面是我用绑定我的组合框柱代码:

    private string CreateDDLColumnEditTemplate(int index, string propertyName, List<Product> ProductList) 
         { 
          StringBuilder CellTemp = new StringBuilder(); 
          CellTemp.Append("<DataTemplate "); 
          CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/"); 
          CellTemp.Append("2006/xaml/presentation' "); 
          CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>"); 
          CellTemp.Append(String.Format("<ComboBox SelectedValue='{{Binding [{0}], Mode=TwoWay}}'>",0)); 
          foreach (Product product in ProductList) 
          { 
           CellTemp.Append("<ComboBoxItem Tag='"+product.ProductGuid+"' Content='" + product.Name + "'></ComboBoxItem>"); 
          } 
          CellTemp.Append(String.Format("</ComboBox>")); 
          CellTemp.Append("</DataTemplate>"); 
          return CellTemp.ToString(); 
         } 
    
    +0

    这些答案是否适合您? – psoshmo

    +0

    你在找什么是'ComboBox.SelectedValuePath'和'ComboBox.SelectedValue'。这两个相结合将做你的尝试。 – Martin

    回答

    3

    你需要的是

    YourComboBox.SelectedValue = currentProduct 
    

    <ComboBox SelectedValuePath="Tag" etc. etc. etc. /> 
    

    这意味着你的SelectedValue从您的Tag

    2

    中获取它的值使用组合框有能力绑定到对象并指定要在下拉列表中显示的内容。为什么不使用要加载到标签中的Product对象,而是在下拉菜单中显示不同的内容?

    如果有需要创建扩展,Partial对象,显示的东西在下拉完全不同,但仍然访问Product项目更改选择。


    这里的建议是简单地将组合框绑定到Product数据项。然后,您可以动态更改该值,而无需使用Tag属性。

    比如我有类似的产品类型数据类型为:

    public class PriceItem 
    { 
        public string Name { get; set; } 
        public int Price { get; set; } 
    } 
    

    这里是保存在我的VM作为Items值的数据列表。

    Items = new List<PriceItem>() 
    { 
        new PriceItem() { Name = "Alpha", Price=100 }, 
        new PriceItem() { Name = "Beta", Price=200 }, 
    }; 
    

    方案 两个组合框,都绑定到Items数据。一个组合框在其下拉菜单中显示Name,而另一个显示PriceName组合框还可以控制价格组合框,当它也改变价格变化时。

    <ComboBox x:Name="comboName" ItemsSource="{Binding Items}" DisplayMemberPath="Name" /> 
    <ComboBox x:Name="comboValue" 
          ItemsSource="{Binding Items}" 
          DisplayMemberPath="Price" 
          SelectedValuePath="Price" 
          SelectedValue="{Binding SelectedItem.Price, ElementName=comboName, Mode=OneWay}" /> 
    

    在操作中,两个组合框均为空白。但是每当我选择第一个时,它就会改变第二个。全部使用相同的数据/数据对象。

    enter image description here

    +0

    这听起来像是最佳的答案,除了我的情况,这是非常复杂的,我不知道我是否可以应用该解决方案。组合框存在于Datagrid列中。 datagrid列是动态加载的,数据源是可观察集合,object []包含每行的字符串数据。最后,我使用列索引绑定了我的datagrid列中的控件。 – user3340627

    +0

    我已经更新了我的问题,并提供了如何在代码背后绑定我的ComboBox的示例代码 – user3340627

    相关问题