2017-09-05 84 views
0

我試着去一個組合框從我的課,我在上下文添加和招投標comboboxEdit的DevExpress

鏈接綁定我有這個在控制

<dxe:ComboBoxEdit Width="100" Margin="5" Name="cboProduct" DisplayMember="Name" 
          SelectedItem="{Binding Path=DataContext.SelectedProduct, Mode=OneWay, RelativeSource={RelativeSource AncestorType=Window}}" 
          > 

     </dxe:ComboBoxEdit> 

It shows this

我充滿了ComboBox befor來自後面的代碼

var lsproducts = new List<Product>(); 
     var Products =_licenseService.GetProductList(); 
     Products.ForEach((x) => { 
      lsproducts.Add(new Product(x.Name, x.ProductId)); 
     }); 

而im設置這樣

[DataMember] 
    public License SelectedLicense { 
     get { return _SelectedLicense;} 
     set {      
      _SelectedLicense = value;     
      this.NotifyPropertyChanged("SelectedLicense"); 
     } 
    }   

    public Product SelectedProduct 
    { 
     get 
     {    
      return new Product(_SelectedLicense.Product.Name,_SelectedLicense.Product.ProductId); 
     }    
    } 

     this.cboProduct.ItemsSource = lsproducts.ToArray(); 
在這兩種情況下

的SelectedProduct即時通訊使用對象產品

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using pd.Common.Domain; 

namespace LicenceManagerWPF.Views 
{ 
public class Product 
{ 
    public string Name { get; set; } 
    public ProductEnum ProductID { get; set; } 

    public Product(string ProductName,ProductEnum ID) 
    { 
     Name = ProductName; 
     ProductID = ID; 
    } 

} 

我不知道爲什麼它不選擇產品時,我打開窗戶。

,我還有一個 [![這樣表示] [2] [2]

我不知道爲什麼它的顯示器的X標誌,但是當我chouse另一許可證更新它的組合選擇

<dxe:ComboBoxEdit x:Name="cboActivationMode" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Width="100" Style="{StaticResource TabMargin}"          
            SelectedItem="{Binding Path=DataContext.SelectedLicense.ActivationMode, RelativeSource={RelativeSource AncestorType=Window}}" 
            /> 

第二個它只有幾個枚舉值填充像這樣。

cboActivationMode.Items.Add(
      new DevExpress.Xpf.Editors.ComboBoxEditItem() 
      { Content = Enum.GetName(typeof(ActivationMode), ActivationMode.Online), Tag = ActivationMode.Online }); 

如何將值綁定到組合框? 問候

 cboActivationMode.Items.Add(   
      new DevExpress.Xpf.Editors.ComboBoxEditItem() 
      {Content = Enum.GetName(typeof(ActivationMode), ActivationMode.Offline),Tag = ActivationMode.Offline}); 

我tryed做這個

public partial class LicenseDetail : UserControl 
{ 
    private readonly LicenseService _licenseService; 
    public LicenseDetail() 
    { 
     InitializeComponent(); 
     _licenseService = new LicenseService();    
     FillCombos(); 
    } 

    private void FillCombos() 
    { 
     FillProducts(); 
     FillActivationMode(); 
     //var str = Enum.GetName(typeof(ProductEnum), ProductEnum.CDSAddIn); 
     //this.cboProduct.ItemsSource = new string[] { str }; 
    } 

    private void FillProducts() 
    { 

     var Products = _licenseService.GetProductList(); 
     cboProduct.ItemsSource = Products;    
    } 

產品列表Iproduct(接口),我不明白爲什麼他們把它像這樣的列表,但每個產品是他們實施diferent相同的基類和接口

[DataContract] 
public class ProductList : List<IProduct> 
{ 
    public bool Contains(ProductEnum productId) 
    { 
     return this.Any(x => x.ProductId == productId); 
    } 

    public IProduct GetProduct(ProductEnum productId) 
    { 
     return this.FirstOrDefault(x => x.ProductId == productId); 
    } 

    public void Remove(ProductEnum productId) 
    { 
     Remove(GetProduct(productId)); 
    } 
} 

我改變組合以結合這樣 的SelectedItem =「{綁定路徑= DataContext.MyProduct,R elativeSource = {的RelativeSource AncestorType =窗口}}」

我創建這樣 公共IProduct myProduct的 { 得到的類屬性{返回_MyProduct; } set { _MyProduct = value; this.NotifyPropertyChanged(「MyProduct」); } }

而且這樣攻擊 _CustomerLicense.MyProduct = SelectedLicense。產品;

這是怎樣的產品清單充滿 公共IProduct GetProduct(ProductEnum的productId) { iProduct產品= NULL;

 var connection = GetConnection(); 

     try 
     { 
      var sql = string.Format(Resources.GetProduct, (int)productId); 
      var cmd = new SqlCommand(sql, connection) { CommandType = CommandType.Text, Transaction = _transaction }; 
      using (var rdr = new NullableDataReader(cmd.ExecuteReader())) 
       while (rdr.Read()) 
       { 
        var productName = rdr.GetString(0); 
        var featureId = rdr.GetInt32(1); 
        var featureDesc = rdr.GetString(2); 
        if (product == null) 
        { 
         switch (productId) 
         { 
          case ProductEnum.PDCalc: 
           product = new PDCalcProduct(productId, productName); 
           break; 
          case ProductEnum.PDMaint: 
           product = new PDMaintProduct(productId, productName); 
           break; 
          case ProductEnum.PBDynamics: 
           product = new PBDynamicsProduct(productId, productName); 
           break; 
          case ProductEnum.CDSAddIn: 
           product = new CDSAddInProduct(productId, productName); 
           break; 
         } 
        } 
        if (product != null) 
         product.Features.Add(new Feature((FeatureEnum)featureId, featureDesc)); 
       } 
     } 
     finally 
     { 
      CloseConnection(connection); 
     } 

     return product; 
    } 

沒有任何運氣。 問候

回答

1

SelectedProduct屬性應該有一個公共的二傳手讓你能夠將它設置爲當前選擇的值在ComboBox

private Product _selectedProduct; 
public Product SelectedProduct 
{ 
    get { return _selectedProduct; } 
    set 
    { 
     _selectedProduct = value; 
     this.NotifyPropertyChanged("SelectedProduct"); 
    } 
} 

而對於初值被選中,你要麼需要將它設置爲一個Product對象,實際上是在ItemsSourcelsproducts):

viewModel.SelectedProduct = lsproducts.FirstOrDefault(x => x.Name == _SelectedLicense.Product.Name && x.ProductID == _SelectedLicense.Product.ProductId); 

或者你將不得不重寫您的Product類的方法:

public class Product 
{ 
    public string Name { get; set; } 
    public ProductEnum ProductID { get; set; } 

    public Product(string ProductName, ProductEnum ID) 
    { 
     Name = ProductName; 
     ProductID = ID; 
    } 

    public override bool Equals(object obj) 
    { 
     Product other = obj as Product; 
     return other != null && Name == other.Name && ProductID == other.ProductID; 
    } 
} 
+0

嗨mm8 我覺得問題在於它的組合裏面有一個控件 –

+0

裏面有一個控件併產品的一個新的對象,我需要在主窗口中這樣設置: _CustomerLicense.SelectedProduct = new Product(SelectedLicense.Product.Name,SelectedLicense.Product.ProductId); 我認爲這是我的問題。 Im填充組合獲取數據庫的產品並創建一個新對象Product,許可證對象的原始數據是一個IProduct,因爲它的接口無法分配 –

+0

您是否試圖按照我的建議覆蓋Equals方法? – mm8

0

我固定這樣的:

我創建的方法:

private void GetProducts() 
     { 

     var Products = new LicenseService().GetProductList(); 
     Products.ForEach((x) => 
     { 
      lsproducts.Add(new Product(x.Name, x.ProductId));    
     }); 
     //this.cboProduct.ItemsSource = lsproducts.ToArray(); 
    } 

然後我連接到主窗口,其中所有的控制的負載是

public frmCustomerLicense(CustomerLicenses cl) 
    { 
     InitializeComponent(); 
     GetProducts(); 
     _CustomerLicense = cl; 
     grdLicenses.grdLicences.SelectedItemChanged += GridRowSelected; 
    } 

然後,當選擇其中一個許可證我將所有的綁定

var Record = (DataRowView)grdLicenses.grdLicences.SelectedItem; 
      var SelectedLicense = (License)Record["License"]; 
      var list = new LicenseService().GetActivityLog(SelectedLicense.SerialNumber) 
      .OrderByDescending(x => x.ActivityDate) 
      .ToList(); 


      _CustomerLicense.ActivityLog = list; 
      _CustomerLicense.Features = new LicenseService().GetFeatures(SelectedLicense.Product.ProductId); 
      _CustomerLicense.Products = lsproducts; 
      _CustomerLicense.HasExpDate = SelectedLicense.HasExpirationDate; 
      //_CustomerLicense.SetLog(list); 
      _CustomerLicense.SelectedLicense = SelectedLicense; 
      //_CustomerLicense.SelectedMaintenance = SelectedLicense.Product.ProductId == ProductEnum.PDMaint ? true : false;         
      _CustomerLicense.SelectedProduct = lsproducts.FirstOrDefault((x) => x.ProductID == SelectedLicense.Product.ProductId); 

在我viewClass類我將此

[DataMember] 
    public Product SelectedProduct 
    { 
     get { return _SelectedProduct; } 
     set 
     {     
      _SelectedProduct = value; 
      this.NotifyPropertyChanged("SelectedProduct"); 
     } 
    } 

    [DataMember] 
    public List<Product> Products 
    { 
     get { return _Products; } 
     set { _Products = value; 
      this.NotifyPropertyChanged("Products"); 
     } 
    }   

所以,我設置了組合框

<dxe:ComboBoxEdit Width="180" Margin="5" Name="cboProduct" DisplayMember="Name" 
          ItemsSource="{Binding Path=DataContext.Products, RelativeSource={RelativeSource AncestorType=Window}}" 
          SelectedItem="{Binding Path=DataContext.SelectedProduct, RelativeSource={RelativeSource AncestorType=Window}}" 
          >     
     </dxe:ComboBoxEdit> 

這樣做的工作,感謝您的幫助MM8