Controlling the Currency of Binded Controls

alslamo alikom...
i did the databinding process to some textboxes in a form and binding mode is two way....
iam asking now about how i would control the concurrency object of the controls....
in another meaning i want to put buttons for next and previous and last record and first record...
is there any class would help me achieve that in WPF...
or should manage that with special code...
thx for ur time...
Eng.Mohammad Abd Elrahman



Answer this question

Controlling the Currency of Binded Controls

  • hezkryvo

    this should help

    ms-help://MS.MSSDK.1033/MS.WinFXSDK.1033/wpf_conceptual/html/fcd37590-bce1-4ac9-8b74-3b96c7458b8a.htm



  • Nands

    WOW....
    thank u guys very much....for ur help...
    but i have a problem in my code that DataContext returns a null when i call it
    here is my code:

    SqlConnection conn =

    new SqlConnection(@"Data Source=BIZCORE\BIZCORE2005;Initial Catalog=SismatixERP;Integrated Security=True");

    SqlDataAdapter adap =

    new SqlDataAdapter("select * from com_m_country", conn);

    ds = new DataSet();

    ds.Tables.Add(new DataTable("table1"));

    adap.Fill(ds, "table1");

    Binding bind = new Binding("NameEn");

    bind.Mode = BindingMode.TwoWay;

    bind.Source = ds.Tables[0];

    this.textBox2.SetBinding(TextBox.TextProperty, bind);

    in the previuos code i did the binding using code there no thin in the XAML file indecating that iam doing binding...

    but when i get the DataContext object either for the Form or for the TextBox object i used...i found DataContext Property is null....

    i realy find ur code is great but help me to use it...or tell me whats wrong with my code....or is there a missing code to fill the DataContext property...by the way i tried the following code but no result still null value:

    this.textBox2.DataContext = bind;

    so sorry for bothering but this is the case....



  • Peter Smith

    Or you can just use the collectionview as the previous user suggested ;)

  • T. Bolon

    Here is a way to do this.

    Create a custom collection class (maybe even generic)

    Add properties on the class that point to the "Current" object of the collection

    Add functions to the collection to move "Current" to first, previous, next, and last.

    Set the datacontext on your form to the Current property of your collection

    here is the code for the custom collection.

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Text;
    using System.ComponentModel;

    namespace CursorCollectionSample
    {
    public class CursorCollection<T>:Collection<T>,INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler PropertyChanged;

    #region Private Fields

    private int m_Current;

    #endregion

    #region
    Public Properties

    public T Current
    {
    get { return this[m_Current]; }
    set{this[m_Current] = value;}
    }

    #endregion

    public T GetNext()
    {
    T tmpCurrent;
    if (m_Current < Count - 1)
    {
    tmpCurrent = this[++m_Current];
    NotifyCurrentChanged();
    }
    else
    {
    tmpCurrent = this[m_Current];
    }
    return tmpCurrent;
    }

    public T GetPrevious()
    {
    T tmpCurrent;
    if (m_Current > 0)
    {
    tmpCurrent = this[--m_Current];
    NotifyCurrentChanged();
    }
    else
    {
    tmpCurrent= this[m_Current];
    }
    return tmpCurrent;
    }

    private void NotifyCurrentChanged()
    {
    if (PropertyChanged != null)
    {
    PropertyChanged(this, new PropertyChangedEventArgs("Current"));
    }
    }

    public void InsertAfter(T item)
    {
    if (m_Current == Count)
    {
    Add(item);
    }
    else
    {
    Insert(m_Current + 1, item);
    }
    }

    public
    void InsertBefore(T item)
    {
    Insert(m_Current, item);
    }
    }
    }

    I made it a generic class so that you can have type safety for any objects. To construct an instance

    CursorCollection<MyClass> myCollection=new CursorCollection<MyClass>();

    (Where MyClass is replaced with the class that will be inserted into the collection.)

    Your only remaining steps are to populate your collection with objects, bind your forms datacontext to your collection's Current property, and make your buttons call GetNext and GetPrevious.



  • Controlling the Currency of Binded Controls