VB.NET - How to get radiobuttons in listbox - Asked By abinav shankar on 05-Sep-11 09:29 AM

Hi

I have to display a list of files in a list view and a user can select only one file out of many how to do it ,how to add radiobutton in listview
smr replied to abinav shankar on 05-Sep-11 09:35 AM
HI

refer this example

<ListBox Name="listBoxDefaultAcc" HorizontalAlignment="Left" VerticalAlignment="Top" Width="450" Height="410">
        <ListBox.ItemTemplate>
          <DataTemplate>
            <StackPanel Orientation="Horizontal" Height="60" Width="450">
              <RadioButton Content="{Binding Name}" IsChecked="{Binding Selected, Mode=TwoWay}" GroupName="defaultAcc" HorizontalAlignment="Left" VerticalAlignment="Center" Height="80" Width="450" />
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
 
public partial class Home : Page
{
  public Home()
  {
    InitializeComponent();
    var items = new List<SomeClass>();
    items.Add(new SomeClass() {Name = "a"});
    items.Add(new SomeClass() {Name = "b"});
    items.Add(new SomeClass() {Name = "c"});
 
    listBoxDefaultAcc.ItemsSource = items;
  }
 
  // Executes when the user navigates to this page.
  protected override void OnNavigatedTo(NavigationEventArgs e)
  {
  }
 
  private void testButton_Click(object sender, RoutedEventArgs e)
  {
    var items = (List<SomeClass>)listBoxDefaultAcc.ItemsSource;
    var selectedItem = items.Where(x => x.Selected).FirstOrDefault();
  }
 
  class SomeClass
  {
    public string Name { get; set; }
    public bool Selected { get; set; }
  }
}

refer
http://stackoverflow.com/questions/4966713/how-do-i-access-a-radiobutton-inside-a-listboxitem-in-windows-phone-7
TSN ... replied to abinav shankar on 05-Sep-11 09:39 AM
hi..

f you use ASP.NET radio buttons, the framework renders each radio button with a distinct name attribute so that they do not function as a group. This means that users can select all the radio buttons, and not just one.

Possible solution

Use ordinary HTML radio buttons bound as follows:

<input type="radio" name="listViewSelect" value='<%# Eval("RecordID") %>' />

You can use any value for the name attribute. Where this code has “RecordID” you need any field you can use to distinguish one item from another. To determine which item was selected, use something like the following to retrieve the value of RecordID:

Dim selection As String = Request.Form("listViewSelect")
dipa ahuja replied to abinav shankar on 05-Sep-11 10:47 AM
If you are asking for windows application then you cannot add radiobuttons inside the listview But you can add the images in the listView

and if you want to allow only select one item at a time then Set the property MultiSelect=False