I am trying to bind Command (which returns a collection of data from entity based on provided Id value) with TextBox_TextChanged event. I do not get any error but the result are not shown in DataGrid. What i am doing wrong?
What I have tried:
<pre>namespace Proba
{
public class Proizvodi
{
[Key] public int Id { get; set; }
[Required] public string Naziv { get; set; }
[Required] public double Cena { get; set; }
}
}
<pre>namespace Proba
{
public class BazaContext:DbContext
{
public DbSet<Proizvodi> Proizvodi { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Baza;");
base.OnConfiguring(optionsBuilder);
}
}
}
My UserControl:
XAML:
<pre lang="c#"><pre><UserControl x:Class="Proba.Kontrola"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Proba"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<local:ViewModel x:Key="vmObj"/>
</UserControl.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding ListaProizvoda, Source={StaticResource vmObj}}"/>
<TextBox x:Name="txtSearch" TextChanged="txtSearch_TextChanged"/>
</StackPanel>
</Grid>
</UserControl>
Code-Behind:
public partial class Kontrola : UserControl
{
public BaseCommand baseCommand
{
get { return (BaseCommand)GetValue(baseCommandProperty); }
set { SetValue(baseCommandProperty, value); }
}
public static readonly DependencyProperty baseCommandProperty =
DependencyProperty.Register("baseCommand", typeof(BaseCommand), typeof(Kontrola), new PropertyMetadata(null));
public Kontrola()
{
InitializeComponent();
}
private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
{
baseCommand.Execute(txtSearch.Text);
}
}
ViewModel class:
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public BaseCommand Komanda { get; set; }
public ViewModel()
{
this.Komanda = new BaseCommand(ProizvodPoId, CanExecute);
}
private void ProizvodPoId(object obj)
{
var context = new BazaContext();
var s = (string)obj;
int id = int.Parse(s);
ListaProizvoda = context.Set<Proizvodi>().Where(e=>e.Id==id).ToList();
}
private bool CanExecute(object obj)
{
return true;
}
private List<Proizvodi> _listaProizvoda;
public List<Proizvodi> ListaProizvoda
{
get { return _listaProizvoda; }
set { _listaProizvoda = value; OnPropertyChanged("ListaProizvoda"); }
}
}
And the MainWindow:
XAML:
<Window x:Class="Proba.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Proba"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:Kontrola baseCommand="{Binding Komanda}">
</local:Kontrola>
</Grid>
</Window>
Code-Behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}