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


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

refer this example

<ListBox Name="listBoxDefaultAcc" HorizontalAlignment="Left" VerticalAlignment="Top" Width="450" Height="410">
            <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" />
public partial class Home : Page
  public Home()
    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; }

TSN ... replied to abinav shankar on 05-Sep-11 09:39 AM

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