c#
  • silverlight
  • combobox
  • 2015-07-10 91 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

    相關問題