Skip to main content
GameDev.net gamedev.net
🔒 Locked

WPF Entity Framework property binding

Started by C3rial Apr 24, 2013 at 9:43 AM 0 replies 3.9k views
Original Post
C3rial
C3rial

Hi!

Thanks in advance for all your help!

I'm trying to wrap my head around Entity Framework and binding.

Also, I'm trying to use MVVM architecture, but that's beside the point.

I've got a small database, which has a view "vwImports".
This view has a field "ProviderName".

I've made entities of this view, and the other objects in the database.

Now, I have a simple UI with a listbox and a textbox.

I've bound the listbox to a CollectionView list of vwImports items,

and I want to bind the textbox to the ProviderName field, so I guess it'll be

the "vwImportsList.CurrentItem.ProviderName" (pseudo).

EDIT: The text-binding of the textbox now is not working, I know.
I've also tried using "Text="{Binding myImports.ProviderName}".

ViewModel code:


public class ImportViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        protected bool SetField<T>(ref T field, T value, string propertyName)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        private ICollectionView _imports;
        public ICollectionView myImports 
        {
            get 
            {
                using (var context = new ImportsContext())
                {
                    context.vwImports.Load();

                    var imports = context.vwImports.Local;
                    _imports = CollectionViewSource.GetDefaultView(imports);
                    return _imports;
                }
            }
            set
            { 
                SetField(ref _imports, value, "myImports"); 
            }
        }

        public ImportViewModel()
        {
            
        }
}

MainWindow.xaml code (Code behind does nothing but make an instance of the viewmodel and set this as datacontext).


<ListBox HorizontalAlignment="Left" Height="300" VerticalAlignment="Top" Width="215" Name="lstImports"
                 DisplayMemberPath="ImportName" ItemsSource="{Binding myImports}" IsSynchronizedWithCurrentItem="True"
                 SelectedValuePath="ImportSeq"/>
        <Label Content="ProviderName:" HorizontalAlignment="Left" Margin="235,24,0,0" VerticalAlignment="Top" Width="91"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="331,27,0,0" 
                 VerticalAlignment="Top" Width="120" Name="txtProviderName" Text="{Binding ElementName=lstImports, Path=myImports.ProviderName}"/>
CrazyPoly
PhillipHamlyn
PhillipHamlyn

Don't include the class name on the binding, just the field name. The binding is all relative to the controls Datacontext (i.e. class to which it is bound).

So; pseudo code




will bind to an IEnumerable with a class field of Person.Surname. Create a collection of Person, set the DataContext property of myList to your collection instance. Should all work.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.