Search code examples
c#wpfxamlwcf-binding

WPF lookupedit binding not woking


I am trying to bind the result from WCF service to devexpress lookupedit.

this is the property I created

<!-- language: c# -->
public class BindingModel
{
private static List < VW_ClientProcess> _clientProcess= new List< VW_ClientProcess>(); 

public List< VW_ClientProcess> clientProcess  
{  
   get  
        {  
            return _clientProcess;  
        }  
        set  
        {  
            _clientProcess = value;  
            OnPropertyChanged("clientProcess");  
        }  
    }    
}  
}    

In WPFApp.xaml.cs

BindingModel bind=new BindingModel();
bind.clientProcess = e.Result.GetClientProcessesResult.ToList< VW_ClientProcess>();  

this is my xaml code(WPFApp.xaml)

<dxlc:LayoutGroup>
   <dxlc:LayoutGroup.DataContext>
      <ViewModel:BindingModel />
    </dxlc:LayoutGroup.DataContext>
   <dxlc:LayoutItem x:Name="liClientProcess"
                         Width="auto"
                         VerticalAlignment="Bottom"
                         Label="Client Process"
                         LabelPosition="Top">

         <dxg:LookUpEdit x:Name="lueClientProcess"    
                     AutoPopulateColumns="True"    
                     DisplayMember="ClientFullName"    
                     ItemsSource="{Binding clientProcess}"    
                     ValueMember="ProcessID" /> 
   </dxlc:LayoutItem>
</dxlc:LayoutGroup>

The problem is when I set ItemSource in xaml, only column names are displaying but the data fields are empty.

but If I set ItemSource through c# code like this

BindingModel bind = new BindingModel();
lueClientProcess.ItemsSource = bind.clientProcess;    

lookupedit edit is getting populated. I am new to WPF. I don't know what I am doing wrong here.


Solution

  • It looks like the problem is you are creating duplicate instances of BindingModel. So here in the XAML creates one instance, and assigns it to the view's DataContext:

    <dxlc:LayoutGroup.DataContext>
      <ViewModel:BindingModel />
    </dxlc:LayoutGroup.DataContext>
    

    But then this doesn't use the existing instance, but creates a new one that isn't attached anywhere to the UI:

    BindingModel bind=new BindingModel();
    bind.clientProcess = e.Result.GetClientProcessesResult.ToList< VW_ClientProcess>();
    

    So I guess what you'd want, instead of the above:

    var bind = (BindingModel)DataContext;
    bind.clientProcess = e.Result.GetClientProcessesResult.ToList< VW_ClientProcess>();