This is a migrated thread and some comments may be shown as answers.

Binding a custom usercontrol GridView

1 Answer 181 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Christophe
Top achievements
Rank 1
Christophe asked on 03 Jun 2014, 12:58 PM
Hello,

I've encountered a problem that looks like so strange and I don't know how to deal with it. I tried a lot of combinations but nothing worked.

I've the "main" class 'MainWindow.cs' which has :

Datas datas = new Datas();
ConfigTabCameras ctc = new ConfigTabCameras(datas);
this.DataContext = datas;

MainWindow.xaml : 1st row doesn't work, 2nd works
<custom:TabCameras />
<telerik:RadGridView ItemsSource="{Binding DatasTabCameras}"/>

Datas.cs :
private ObservableCollection<ViewTabCameras> datasTabCameras;
 
public ObservableCollection<ViewTabCameras> DatasTabCameras
{
    get
    {
        if (this.datasTabCameras == null)
        {
            this.datasTabCameras = new ObservableCollection<ViewTabCameras>();
        }
 
        return this.datasTabCameras;
    }
    set
    {
        this.EventPropertyChanged("DatasTabCameras");
    }  
}
 
public Datas()
{
 
}

And the inheritance of INotifyPropertyChanged etc...

ConfigTabCameras :
public class ConfigTabCameras : AbstractSql
    {
        public ConfigTabCameras(Datas datas)
        {
            this.configurationDatas = datas;
            ConfigurationTabCameras();
        }
 
        private void TabCamerasConfiguration()
        {
            connect.dbConnect();
 
            command.CommandText = "SELECT f1, f2, f3 " +
                  "FROM t1ORDER BY f1";
            command.CommandType = CommandType.Text;
            command.Connection = connect.getSqlConnection();
 
            dataReader = command.ExecuteReader();
 
            while (dataReader.Read())
            {
                ViewTabCameras vtc = new ViewTabCameras();
 
                vtc.Field1 = dataReader["f1"].ToString();
                vtc.Field2 = int.Parse(dataReader["f2"].ToString());
                vtc.Field3 = int.Parse(dataReader["f3"].ToString());
                 
                donneesConfiguration.DatasTabCameras.Add(vtc);
            }
             
            command.Dispose();
            dataAdapter.Dispose();
 
            connect.dbDisconnect();
        }
    }

ViewTabCameras.cs :
private string field1;
private int field2;
private int field3;
 
public string Field1
{
    get
    {
        return this.field1;
    }
    set
    {
        this.field1= value;
        this.EventPropertyChanged("Field1");
    }
}
 
// Same for Field2 and 3...


And finally my custom UserControl as a GridView TabCameras.cs :
private Donnees donnees;
 
public TabCameras()
{
    datas = new Datas();
    //this.DataContext = datas;
    InitializeComponent();
}

TabCameras.xaml :
<telerik:RadGridView ItemsSource="{Binding DatasTabCameras}">
        <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Field1}"
                                            Header="Field 1"
                                            UniqueName="Field1" />
                <telerik:GridViewComboBoxColumn Header="Field 2"
                                                ItemsSource="{Binding Field2}"
                                                UniqueName="Field2"
                                                DataMemberBinding="{Binding Field2}" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Field3}"
                                                Header="Field 3"
                                                UniqueName="Field3" />
        </telerik:RadGridView.Columns>
</telerik:RadGridView>


So the MainWindow is supposed to include my custom controls but they don't display anything. However, when I add a GridView control in my MainWindow with the same binding, it works perfectly. I tried to change the position of "dataContext" in many classes but nothing changed.

So, how can I have my custom controls in my MainWindow which display everything returned by my SQL query and saved in my Datas.cs please ?

I hope I was clear enough, I don't think I used the easier way...

Thanks !

1 Answer, 1 is accepted

Sort by
0
Christophe
Top achievements
Rank 1
answered on 04 Jun 2014, 01:15 PM
Ok I just did it, I don't know how I get the idea but it works... If you want to know, just don't put your new instances of your SQL classes in your MainWindow's constructor, but in Datas' like that :

public Datas()
{
    SqlClassA A = new SqlClassA(this);
}

And then in your SqlClassA :
public SqlClassA(Datas datas)
{
    ...
    datas.ObservableCollectionToFill.Add(element);
    ...
}

It's just an example of how to use it, I wouldn't advise you to put something like that in the constructor. I don't really know why it works like that though...
Tags
GridView
Asked by
Christophe
Top achievements
Rank 1
Answers by
Christophe
Top achievements
Rank 1
Share this question
or