Click here to Skip to main content
14,828,290 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
<pre>namespace Proba
{
    /// <summary>
    /// ////            Entity Class
    /// </summary>
    public class Proizvodi
    {
        [Key] public int Id { get; set; }
        [Required] public string Naziv { get; set; }
        [Required] public double Cena { get; set; }
    }
}

C#
<pre>namespace Proba
{
    public class BazaContext:DbContext
    {
        /// <summary>
        /// //              Database context class
        /// </summary>
        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:
XML
<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:
C#
public partial class Kontrola : UserControl
    {

        public BaseCommand baseCommand
        {
            get { return (BaseCommand)GetValue(baseCommandProperty); }
            set { SetValue(baseCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for baseCommand.  This enables animation, styling, binding, etc...
        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:
C#
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;// i was setting breakpoint here and values from text box are here and converted
            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:
XML
<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:
C#
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new ViewModel();
        }
    }
Posted
Updated 4-Jan-21 5:06am

You should install the Microsoft.Xaml.Behaviors.Wpf package.
GitHub - microsoft/XamlBehaviorsWpf: Home for WPF XAML Behaviors on GitHub.[^]
There are a lot of examples how to convert events to commands.
   
I've solved the problem. In my user control i set the datacontext of datagrid as static resource. I changed it and set Data Context in user control constructor, and everything works fine.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900