1

I'm looking for errors in my code. The sql query returns over 200 items, but does not see any. I have this FiltryView.xaml

<UserControl
         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:Famex2.View"          
         xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:dxdo="http://schemas.devexpress.com/winfx/2008/xaml/docking" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxlc="clr-namespace:DevExpress.XtraPrinting.Export;assembly=DevExpress.Printing.v16.2.Core"
xmlns:dxlc1="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" x:Class="Famex2.View.FiltryView"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="3000">

<Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="300"/>
            <RowDefinition Height="19*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="310"/>
            <ColumnDefinition Width="300"/>
            <ColumnDefinition Width="300"/>
            <ColumnDefinition Width="1000*"/>
        </Grid.ColumnDefinitions>


    <local:GrupaKartotekFiltrView Grid.Column="0" Grid.Row="0" />
    <local:GrupaKartotekowaAsortymentView Grid.Column="1"  Grid.Row="0"/>
        <local:ZakresDatRozchodowView Grid.Column="2"  Grid.Row="0"/>
        <local:DodatkoweFiltryView Grid.Column="2" Grid.Row="0" VerticalAlignment="Bottom"/>
        <local:UczestniczyW1View Grid.Column="3" Grid.Row="0"/>

        <dx:SimpleButton Content="Oblicz" 
                     Grid.Column="3" 
                     HorizontalAlignment="Left" 
                     Height="29" 
                     Margin="10,261,0,0" 
                     VerticalAlignment="Top" 
                     Width="75" Click="SimpleButton_Click"/>

</Grid>

and his class

 public partial class FiltryView : UserControl
{
    public FiltryView()
    {
        InitializeComponent();
    }

    private void SimpleButton_Click(object sender, RoutedEventArgs e)
    {
        ParametryWyjscioweViewModel.Instance.startBackgroundProcess();
    }
}

second view

<UserControl
         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:Famex2.View"
         xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" x:Class="Famex2.View.ParametryWyjscioweView"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <dxg:GridControl x:Name="ParametryWyjscioweGrid" 
        ItemsSource="{Binding ListaParametryWyjsciowe}" 
                     AutoGenerateColumns="AddNew">
        <dxg:GridControl.View>
            <dxg:TableView x:Name="view4"  AllowEditing="False"
                             AllowConditionalFormattingManager="True" AllowConditionalFormattingMenu="True"  />
        </dxg:GridControl.View>
    </dxg:GridControl>
</Grid>

his viewmodel:

 public class ParametryWyjscioweViewModel : INotifyPropertyChanged
{
    public List<ParametrWyjsciowy> ListaParametryWyjsciowe { get; set; }
    private static ParametryWyjscioweViewModel _instance = new ParametryWyjscioweViewModel();
    public static ParametryWyjscioweViewModel Instance { get { return _instance; } }

    BackgroundWorker _worker;
    public int _progress = 20;
    public int Progress
    {
        get { return _progress; }
        set
        {
            _progress = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Progress"));
        }
    }
    public void startBackgroundProcess()
    {
        _worker = new BackgroundWorker();
        _worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        _worker.ProgressChanged += worker_Progress_Changed;
        _worker.RunWorkerAsync();
    }


    public ParametryWyjscioweViewModel()
    {
        ListaParametryWyjsciowe = new List<ParametrWyjsciowy>();

    }



    private void worker_Progress_Changed(object sender, ProgressChangedEventArgs e)
    {
        Progress = e.ProgressPercentage;
    }

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        ListaParametryWyjsciowe.Clear();
        XmlReader xmlReader = new XmlReader(System.Environment.CurrentDirectory + @"\SqlConfig.xml");
        string sql = xmlReader.GetElementValue(0, "ParametryWyjsciowe");
        DataTable dt = DataBaseManager.ExecuteQueryResult(sql);

        foreach (DataRow dr in dt.Rows)
        {
            ParametrWyjsciowy parametr = new ParametrWyjsciowy();
            parametr.IdKartoteka = int.Parse(dr["id_kartoteka"].ToString());
            parametr.NazwaSkr = dr["nazwaskr"].ToString();
            parametr.NazwaDl = dr["nazwadl"].ToString();
            ListaParametryWyjsciowe.Add(parametr);
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
}

Also his class

public partial class ParametryWyjscioweView : UserControl
{
    public ParametryWyjscioweView()
    {
        InitializeComponent();

        DataContext = new ParametryWyjscioweViewModel();

    }

}

I have a public static instance (ParametryWyjscioweViewModel) and I am using it in the onClick function of the button in the view class (FiltryView). When I got a sql query in the constructor it worked, but I want it to happen after clicking the button. Any ideas ?

Rafal612
  • 23
  • 4

2 Answers2

1

Set the DataContext to the instance returned from the Instance property. It is this one that you populate with data:

public ParametryWyjscioweView()
{
    InitializeComponent();
    DataContext = ParametryWyjscioweViewModel.Instance;
}

You should also raise the PropertyChanged event for the List<T> property or use an ObservableCollection<T>:

public class ParametryWyjscioweViewModel : INotifyPropertyChanged
{
    public List<ParametrWyjsciowy> ListaParametryWyjsciowe { get; set; }
    private static ParametryWyjscioweViewModel _instance = new ParametryWyjscioweViewModel();
    public static ParametryWyjscioweViewModel Instance { get { return _instance; } }

    BackgroundWorker _worker;
    public int _progress = 20;
    public int Progress
    {
        get { return _progress; }
        set
        {
            _progress = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Progress"));
        }
    }
    public void startBackgroundProcess()
    {
        _worker = new BackgroundWorker();
        _worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        _worker.ProgressChanged += worker_Progress_Changed;
        _worker.RunWorkerAsync();
    }

    private ParametryWyjscioweViewModel()
    {
        ListaParametryWyjsciowe = new List<ParametrWyjsciowy>();
    }

    private void worker_Progress_Changed(object sender, ProgressChangedEventArgs e)
    {
        Progress = e.ProgressPercentage;
    }

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        XmlReader xmlReader = new XmlReader(System.Environment.CurrentDirectory + @"\SqlConfig.xml");
        string sql = xmlReader.GetElementValue(0, "ParametryWyjsciowe");
        DataTable dt = DataBaseManager.ExecuteQueryResult(sql);

        List<ParametrWyjsciowy> temp = new List<ParametrWyjsciowy>();
        foreach (DataRow dr in dt.Rows)
        {
            ParametrWyjsciowy parametr = new ParametrWyjsciowy();
            parametr.IdKartoteka = int.Parse(dr["id_kartoteka"].ToString());
            parametr.NazwaSkr = dr["nazwaskr"].ToString();
            parametr.NazwaDl = dr["nazwadl"].ToString();
        }

        Application.Current.Dispatcher.BeginInvoke(new Action(()=> 
        {
            ListaParametryWyjsciowe = temp;
            OnPropertyChanged("ListaParametryWyjsciowe");
        }));
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
0

Where do you INotifyPropertyChanged your Bindings that your internal List was added to / populated by new Elements from the Background worker?

Maybe use ObservableCollection instead of List.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69