WPF Control Arrays?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: WPF Control Arrays?

  1. #1
    Join Date
    Jan 2006
    Posts
    35

    WPF Control Arrays?

    I've got a block of controls I am using multiple times on different tabs. I created a user control in XAML so that I could re-use this block. If I make a aesthetics change to it then it propagates through with ease. Now, I need to initialize the controls in the user control. That's easy enough. However, instead of doing this:

    Code:
                SegmentControl1.ComboBoxMode.ItemsSource = fc.CameraSettings.Modes;
                SegmentControl1.ComboBoxMode.DisplayMemberPath = "Value";
                SegmentControl1.ComboBoxMode.SelectedValuePath = "Key";
                //1 to 10 ...
                SegmentControl10.ComboBoxMode.ItemsSource = fc.CameraSettings.Modes;
                SegmentControl10.ComboBoxMode.DisplayMemberPath = "Value";
                SegmentControl10.ComboBoxMode.SelectedValuePath = "Key";
    I would like to do something similar to this:

    Code:
                //Loop i from 1 to 10
                SegmentControl[i].ComboBoxMode.ItemsSource = fc.CameraSettings.Modes;
                SegmentControl[i].ComboBoxMode.DisplayMemberPath = "Value";
                SegmentControl[i].ComboBoxMode.SelectedValuePath = "Key";
    This would be much more efficient and result in code easier to read.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,383

    Re: WPF Control Arrays?

    I would try to get away from initializing controls as much as possible in code behind and use data binding instead.

    In addition rather than using a fixed number of controls in an array, I would use a custom list and bind the array to the list (in other words, each SegmentControl would be a list item).

    The advantage of this approach is that you would end up with very little initialization code in the code behind.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center