Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. UserControl Databinding

UserControl Databinding

Scheduled Pinned Locked Moved WPF
wpftutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikla521
    wrote on last edited by
    #1

    I give up, can't find any good information about this anywhere :( I have a UserControl with 6 textboxes. I have a quite complex datastructure from where I want to fill these textboxes. The datastructure contains several sets of "String[6]" but I can't figure out how to bind it :(

    public partial class MyAttBox : UserControl
    {
    // 6 of these with different names
    public static DependencyProperty LowChRXProperty = DependencyProperty.Register("LowChRX", typeof(String), typeof(MyAttBox));
    public String LowChRX
    {
    get { return (String)GetValue(LowChRXProperty); }
    set { SetValue(LowChRXProperty, value); }
    }
    }

    "main" XAML:

    <AttBox:MyAttBox Header="GSM 850" x:Name="AttBox_GSM850" />

    my data is stored like:

    public class ABC
    {
    private Dictionary<String, List<String>> m_values = new Dictionary<String, List<String>>();

        public Dictionary<String, List<String>> Values
        {
            get { return m\_values; }
            set { m\_values = value; }
        }
    

    What I want to is to select a Key in my code and connect the Values to the texboxes in my UC. How?

    M G 2 Replies Last reply
    0
    • M mikla521

      I give up, can't find any good information about this anywhere :( I have a UserControl with 6 textboxes. I have a quite complex datastructure from where I want to fill these textboxes. The datastructure contains several sets of "String[6]" but I can't figure out how to bind it :(

      public partial class MyAttBox : UserControl
      {
      // 6 of these with different names
      public static DependencyProperty LowChRXProperty = DependencyProperty.Register("LowChRX", typeof(String), typeof(MyAttBox));
      public String LowChRX
      {
      get { return (String)GetValue(LowChRXProperty); }
      set { SetValue(LowChRXProperty, value); }
      }
      }

      "main" XAML:

      <AttBox:MyAttBox Header="GSM 850" x:Name="AttBox_GSM850" />

      my data is stored like:

      public class ABC
      {
      private Dictionary<String, List<String>> m_values = new Dictionary<String, List<String>>();

          public Dictionary<String, List<String>> Values
          {
              get { return m\_values; }
              set { m\_values = value; }
          }
      

      What I want to is to select a Key in my code and connect the Values to the texboxes in my UC. How?

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      I'm confused...

      mikla521 wrote:

      The datastructure contains several sets of "String[6]" but I can't figure out how to bind it

      Then why didn't you show these "sets of "String[6]""?? :) The 6 textboxes....you want to bind those by index to 6 strings in a collection, right? And that collection of 6 strings - where is that? Is that the List<> in each dictionary entry?

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      M 1 Reply Last reply
      0
      • M Mark Salsbery

        I'm confused...

        mikla521 wrote:

        The datastructure contains several sets of "String[6]" but I can't figure out how to bind it

        Then why didn't you show these "sets of "String[6]""?? :) The 6 textboxes....you want to bind those by index to 6 strings in a collection, right? And that collection of 6 strings - where is that? Is that the List<> in each dictionary entry?

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        M Offline
        M Offline
        mikla521
        wrote on last edited by
        #3

        Ok, will try refine myself here :)

        public class CouplerDB
        {
        private Dictionary<String, Coupler> m_products;
        ....
        }

        public class Coupler
        {
        private Dictionary<String, Attenuation> m_attenuations
        ....
        }

        public class Attenuation
        {
        private Dictionary<Band, List<String>> m_values = new Dictionary<Band, List<String>>();

        I want my user control textboxes to display the values from the List<String> but it's not absolute necessary to have that list, just a place to store 6 different value. Scenario: In a listbox I can select a product. Radiobuttons to select Coupler. Then I have a UserControl for each Band which I want to display the corresponding Strings. Clear? :-D *Sigh* If I change Coupler I need to change the binding. Another way to do it is to hardcode everything, but then I need almost 60 UserControls with a total of 360 Textboxes! My way I "only" need maximum 10 UserControls...

        M 1 Reply Last reply
        0
        • M mikla521

          Ok, will try refine myself here :)

          public class CouplerDB
          {
          private Dictionary<String, Coupler> m_products;
          ....
          }

          public class Coupler
          {
          private Dictionary<String, Attenuation> m_attenuations
          ....
          }

          public class Attenuation
          {
          private Dictionary<Band, List<String>> m_values = new Dictionary<Band, List<String>>();

          I want my user control textboxes to display the values from the List<String> but it's not absolute necessary to have that list, just a place to store 6 different value. Scenario: In a listbox I can select a product. Radiobuttons to select Coupler. Then I have a UserControl for each Band which I want to display the corresponding Strings. Clear? :-D *Sigh* If I change Coupler I need to change the binding. Another way to do it is to hardcode everything, but then I need almost 60 UserControls with a total of 360 Textboxes! My way I "only" need maximum 10 UserControls...

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          mikla521 wrote:

          Clear?

          No. ;P

          mikla521 wrote:

          Radiobuttons to select Coupler

          Select a coupler from where? You only show one coupler per product in the m_products dictionary. How do you know how many radiobuttons there should be? Are any of these dictionaries fixed count? Which ones? It loks on the outside like a (relatively) simple 3-level master-detatil scenario. I just want to try to put together a sample for you and it would be easiest if I know and understand the data you're working with :)

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          M 1 Reply Last reply
          0
          • M Mark Salsbery

            mikla521 wrote:

            Clear?

            No. ;P

            mikla521 wrote:

            Radiobuttons to select Coupler

            Select a coupler from where? You only show one coupler per product in the m_products dictionary. How do you know how many radiobuttons there should be? Are any of these dictionaries fixed count? Which ones? It loks on the outside like a (relatively) simple 3-level master-detatil scenario. I just want to try to put together a sample for you and it would be easiest if I know and understand the data you're working with :)

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            M Offline
            M Offline
            mikla521
            wrote on last edited by
            #5

            I will try again :) A listbox where I can select product, nothing strange here. Then a Tab with 6 radiobuttons to select Coupler and in that same Tab I have my UserControls. So when I change selectedItem in my listbox OR change radiobutton, I need to update my UserControls with new data. ONE of the UserControls in my XAML looks like:

            <AttBox:MyAttBox Header="GSM 850" x:Name="AttBox_GSM850"/>

            In my code I have (don't care about the private members now, let's say they are public :) ):

            public CouplerDB myDB = new CouplerDB

            When I want to change in the UC I get the values from:

            Product p = (Product)productListbox.SelectedItem;
            myDB.m_products[p.ProductName].m_attenuations["Cable"].Values[Band.GSM850][0]
            myDB.m_products[p.ProductName].m_attenuations["Cable"].Values[Band.GSM850][1]...

            so I can set

            AttBox_GSM850.LowChRX = myDB.m_products[p.ProductName].m_attenuations["Cable"].Values[Band.GSM850][0];

            But to simplify things here... If I have ONE and only ONE UC. I want a Dictionary where each KEY selects, i.e a List<String> to populate the UC listboxes, also want TwoWayBinding. Ehh..I have a testproject, where can I post it? :)

            M 1 Reply Last reply
            0
            • M mikla521

              I will try again :) A listbox where I can select product, nothing strange here. Then a Tab with 6 radiobuttons to select Coupler and in that same Tab I have my UserControls. So when I change selectedItem in my listbox OR change radiobutton, I need to update my UserControls with new data. ONE of the UserControls in my XAML looks like:

              <AttBox:MyAttBox Header="GSM 850" x:Name="AttBox_GSM850"/>

              In my code I have (don't care about the private members now, let's say they are public :) ):

              public CouplerDB myDB = new CouplerDB

              When I want to change in the UC I get the values from:

              Product p = (Product)productListbox.SelectedItem;
              myDB.m_products[p.ProductName].m_attenuations["Cable"].Values[Band.GSM850][0]
              myDB.m_products[p.ProductName].m_attenuations["Cable"].Values[Band.GSM850][1]...

              so I can set

              AttBox_GSM850.LowChRX = myDB.m_products[p.ProductName].m_attenuations["Cable"].Values[Band.GSM850][0];

              But to simplify things here... If I have ONE and only ONE UC. I want a Dictionary where each KEY selects, i.e a List<String> to populate the UC listboxes, also want TwoWayBinding. Ehh..I have a testproject, where can I post it? :)

              M Offline
              M Offline
              mikla521
              wrote on last edited by
              #6

              Been trying around a little... If my data object looks like:

              public class MyData : INotifyPropertyChanged
              {
              private String m_LowChRX;
              private String m_LowChTX;

              public String LowChRX
              {
              get { return m_LowChRX; }
              set { m_LowChRX = value; OnPropertyChanged("LowChRX"); }
              }
              public String LowChTX
              {
              get { return m_LowChTX; }
              set { m_LowChTX = value; OnPropertyChanged("LowChTX"); }
              }...

              In my code I can set the data values in

              MyData myData1;

              And use binding like this

              binding = new Binding("LowChRX");
              binding.Source = myData1;
              myBox.tbLowChRX.SetBinding(TextBox.TextProperty, binding);

              where myBox is my UC. And, again, I have 6 textboxes in myBox. Then I have to repeat that binding for each textbox. And here is the point where I change from myData1 to other data objects. If I have a List<String> in my UC, how do I bind it? When then TextBox is a Control, maybe I have to make a custom control inside which hold my List? So I can bind like:

              myBox.CustomControl.SetBinding(Property, binding)

              Am I on the right track now? :)

              K 1 Reply Last reply
              0
              • M mikla521

                I give up, can't find any good information about this anywhere :( I have a UserControl with 6 textboxes. I have a quite complex datastructure from where I want to fill these textboxes. The datastructure contains several sets of "String[6]" but I can't figure out how to bind it :(

                public partial class MyAttBox : UserControl
                {
                // 6 of these with different names
                public static DependencyProperty LowChRXProperty = DependencyProperty.Register("LowChRX", typeof(String), typeof(MyAttBox));
                public String LowChRX
                {
                get { return (String)GetValue(LowChRXProperty); }
                set { SetValue(LowChRXProperty, value); }
                }
                }

                "main" XAML:

                <AttBox:MyAttBox Header="GSM 850" x:Name="AttBox_GSM850" />

                my data is stored like:

                public class ABC
                {
                private Dictionary<String, List<String>> m_values = new Dictionary<String, List<String>>();

                    public Dictionary<String, List<String>> Values
                    {
                        get { return m\_values; }
                        set { m\_values = value; }
                    }
                

                What I want to is to select a Key in my code and connect the Values to the texboxes in my UC. How?

                G Offline
                G Offline
                Gideon Engelberth
                wrote on last edited by
                #7

                WPF gives you a couple options here. I'll just show them in XAML and let you decide what you want and translate to code-behind if you want. All of these examples assume you have an object of type ABC as the DataContext. Option A - Bind to a specific list with a known key:

                <ListBox ItemsSource="{Binding Values[KeyToGetItems]}" />

                Option B - Bind to the entire list with another set of controls for the current item:

                <Grid>
                <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <ListBox Name="lstValues" ItemsSource="{Binding Values}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Key}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                
                <StackPanel Grid.Row="1" ItemsSource="{Binding SelectedItem.Value, ElementName=lstValues}">
                    <!-- other controls here -->            
                </StackPanel>
                

                </Grid>

                1 Reply Last reply
                0
                • M mikla521

                  Been trying around a little... If my data object looks like:

                  public class MyData : INotifyPropertyChanged
                  {
                  private String m_LowChRX;
                  private String m_LowChTX;

                  public String LowChRX
                  {
                  get { return m_LowChRX; }
                  set { m_LowChRX = value; OnPropertyChanged("LowChRX"); }
                  }
                  public String LowChTX
                  {
                  get { return m_LowChTX; }
                  set { m_LowChTX = value; OnPropertyChanged("LowChTX"); }
                  }...

                  In my code I can set the data values in

                  MyData myData1;

                  And use binding like this

                  binding = new Binding("LowChRX");
                  binding.Source = myData1;
                  myBox.tbLowChRX.SetBinding(TextBox.TextProperty, binding);

                  where myBox is my UC. And, again, I have 6 textboxes in myBox. Then I have to repeat that binding for each textbox. And here is the point where I change from myData1 to other data objects. If I have a List<String> in my UC, how do I bind it? When then TextBox is a Control, maybe I have to make a custom control inside which hold my List? So I can bind like:

                  myBox.CustomControl.SetBinding(Property, binding)

                  Am I on the right track now? :)

                  K Offline
                  K Offline
                  Kunal Chowdhury IN
                  wrote on last edited by
                  #8

                  You can use Binding with the DependencyProperty. You can get information on how to implement the DependencyProperty in this blog: How to Implement a DependencyProperty? Try with that. It will work.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups