You must Sign In to post a response.
  • Category: WPF

    Select Default Radio Buuton

    Hi,
    I was added group of radio buttons in DataTemplate ( assume here 10 radio buttons are added dynamically). Now I would need to check one radio button as bydeault checked, how can I achieve this one programmatically or XAML ( WPF).

    Sample Code:
    <ListBox Name="skinChangeListBox" Height="100" Background="Transparent"
    BorderThickness="0"
    ItemContainerStyle="{StaticResource SkinChangeListBoxItem}">
    <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
    <VirtualizingStackPanel IsItemsHost="True" Orientation="Horizontal"/>
    </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
    <DataTemplate>
    <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,0" Height="96">
    <RadioButton IsChecked="True" GroupName="Theme" Content="{Binding Name}" Margin="10,0,0,0" Checked="rdoDefault_Checked"
    Style="{StaticResource SettingRadioButton}" Height="22" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </StackPanel>
    </DataTemplate>
    </ListBox.ItemTemplate>

    </ListBox>
  • #768756
    Hai Rahul,
    You can create a property and then bind the property value with the IsChecked.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #768766
    The RadioButton.GroupName Property is a DependencyProperty, so that means that you can data bind unique values for each group:

    <DataTemplate>
    <StackPanel Name="stk">
    <StackPanel Margin="5,2,0,0">
    <WrapPanel Margin="10,10,10,5" Height="Auto">
    <TextBlock TextWrapping="Wrap" x:Name="tbC" FontSize="18" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black" Text="Question : "/>
    <TextBlock TextWrapping="Wrap" Text="{Binding NOIDUNG}" FontSize="18" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black"/>
    </WrapPanel>
    <WrapPanel Margin="10,5,10,5" Height="Auto">
    <RadioButton x:Name="rdoA" GroupName="{Binding GroupName}"/>
    <TextBlock TextWrapping="Wrap" Name="A" Text="{Binding A}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
    <RadioButton x:Name="rdoB" GroupName="{Binding GroupName}" Margin="10,0,0,0"/>
    <TextBlock TextWrapping="Wrap" Name="B" Text="{Binding B}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
    </WrapPanel>
    <WrapPanel Margin="10,5,10,5" Height="Auto">
    <RadioButton x:Name="rdoC" GroupName="{Binding GroupName}"/>
    <TextBlock TextWrapping="Wrap" Name="C" Text="{Binding C}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
    <RadioButton x:Name="rdoD" GroupName="{Binding GroupName}" Margin="10,0,0,0"/>
    <TextBlock TextWrapping="Wrap" Name="D" Text="{Binding D}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
    </WrapPanel>
    </StackPanel>
    </StackPanel>
    </DataTemplate>

    To make this example work, you would just need to add a GroupName property into the view model that you are data binding to in the DataTemplate, ie. the object that has the A, B, C and D properties in.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #768795
    You need check the radiobutton in the radiogroup as
     radiogroup.check(IdOfYourButton)with radio buttion ID radioButton.getId()
    Or android:checkedButton field in your RadioGroup
    <RadioGroup
    ....
    android:checkedButton="@+id/button_1">

    <RadioButton
    android:id="@+id/button_1"
    ...../>

    <RadioButton
    android:id="@+id/button_2"
    ...../>

    <RadioButton
    android:id="@+id/button_3"
    ...../>
    </RadioGroup>


  • Sign In to post your comments