2017-09-05 5 views
0

イムiは、コンテキスト追加し、結着comboboxEdit DevExpress社

にリンクされ、私のクラスから1つのコンボボックスをバインドしようとしている私はbeforコンボボックスを埋め

<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

コントロールでこれを持っていますこのようなコードの背後から

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

どちらの場合も、この

[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; 
    } 

} 

を使用してイム。

は、私は別の1 持っている[を!このような[ショー] [2] [2]私は、なぜその表示×印を知らないが、私は別のライセンスを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}}" 
            /> 

2番目のものは、このような数の列挙型の値です。

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}); 

私はこの

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です私はこの のSelectedItem = "{バインディングパス= DataContext.MyProduct、Rように結合することがコンボを変更し、同じ基底クラスとインタフェース

[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)); 
    } 
} 

elativeSource = {RelativeSource AncestorType =ウィンドウ}}」

私はこの 公共IProduct MYPRODUCT { のget {リターン_MyProductのようなクラスのプロパティを作成します。 } セット { _MyProduct = value; this.NotifyPropertyChanged( "MyProduct"); }}

そしてこの _CustomerLicense.MyProduct = SelectedLicenseようassaing。製品;

これは製品のリストが充填されている方法である 公共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"); 
    } 
} 

そして、選択されるべきintial値のために、あなたのいずれかが必要ItemsSourcelsproducts)に実際にあるProductオブジェクトに設定します

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

それとも、をオーバーライドする必要がありますあなたのProductクラスの方法:

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 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); これは私の問題だと思います。 データベースの製品を取得し、新しいオブジェクトProductを作成するコンボを埋めて、ライセンスオブジェクトのオリジナルは、そのインタフェースが割り当てられていないため、割り当てられないIProductです –

+0

私が示唆したようにEqualsメソッドをオーバーライドしようとしましたか? – mm8

0

は、私はこのような固定are

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

に感謝を追加しました
関連する問題