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}"/>