CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2004
    Posts
    25

    Binding Text & Radio Buttons to a List

    I've successfully bound two strings to a list, but am having trouble figuring out how to bind text with 2 radio buttons properly.

    I have a zone array of 8 bytes, that holds On/Off values of the zone. There are 64 zones, thus the 8 bytes. So, bit 0 of byte 0 is '1', if the zone is ON, '0' if OFF.

    I have limited space in a Grid, so I want to use a List or some other control that has automatic scrolling.

    Something like:

    Zone 1 ON<RB> OFF<RB>
    Zone 2 ON<RB> OFF<RB>
    .
    .
    Zone 63 ON<RB> OFF<RB>
    Zone 64 ON<RB> OFF<RB>

    I don't need any NotifyPropertyChanges, because when the user clicks on the SAVE button, the 8 bytes get resaved with the current settings.

    I tried some things as I did before with string binding (see code), but this syntax isn't correct, and I don't know if I'm doing it correctly for Radio Buttons. Or maybe a list isn't the correct control.

    List<ZoneEntry> zentry = new List<ZoneEntry>();

    public class ZoneEntry
    {
    public string ZoneNum { get; set; }
    public RadioButton ZoneOff;
    public RadioButton ZoneOn;
    }

    void CreateZoneTable()
    {
    for (byte i = 0; i < 64; i++)
    {
    zentry.Add(new ZoneEntry()
    {
    ZoneNum = "Zone " + (i + 1),
    ZoneOff = ???
    ZoneOn = ???
    });
    }
    ZoneTable.ItemsSource = zentry;
    }

    <DataGrid Grid.Column="0" Grid.Row="1" Height="150" Width="300"
    CanUserAddRows="false"
    AutoGenerateColumns ="false"
    HorizontalAlignment="Left"
    RowHeight ="20"
    Margin="15,10,0,0"
    VerticalAlignment="Top"
    Name="ZoneTable"
    CanUserSortColumns="False"
    CanUserResizeColumns="False"
    CanUserReorderColumns="False"
    CanUserResizeRows="False"
    SelectionUnit="Cell">

    <DataGrid.Columns>
    <DataGridTextColumn Header = "Zone #"
    Width = "SizeToHeader"
    Binding = "{Binding ZoneNum}"
    IsReadOnly="true"/>

    <DataGridTextColumn Header = "Zone Setting"
    Binding = "{Binding ZoneOn}"
    Binding = "{Binding ZoneOff}"
    Width="*"/>
    </DataGrid.Columns>
    </DataGrid>

    What I want to do is set the RB to checked, if the proper zone[i] byte and bit is set to '1' when the table loads. When the user clicks SAVE, the values from zentry are converted back into the zone[i] array.

    The WPF binding isn't right, because I don't know how to bind 2 RB under the same column.

    Any help on the simplest approach or corrections/additions to the above?

    Thanks.

    Sutton

  2. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Binding Text & Radio Buttons to a List

    Quote Originally Posted by dodge55 View Post
    The WPF binding isn't right, because I don't know how to bind 2 RB under the same column.

    Any help on the simplest approach or corrections/additions to the above?
    The simplest solution, given the possible choices (on/off), is to simply bind to one of those RadioButton-s (since you can infer the state of the other).
    Also, it's probably better to use a CheckBox for something like this.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured