I have created radio button dynamically in ItemsControl through XMLDataProvider binding.
Also, I want to set the values for each radio button (from XML) and when I check on a radio button, I have to manipulate with the corresponding value.
Could you please help me how on this asap?
Loading
Master BillaPosted Aug 13, 2009, 4:16 AM
i hope my answer is help to you, if it mark it answer. thank you
Master BillaPosted Aug 12, 2009, 11:30 PM
Hi
I have give full complete my code to you,
XAML
<Window x:Class="DataBinding.RadioButtonList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="RadioButtonList" Height="300" Width="300"
>
<Window.Resources>
<Style x:Key="RadioButtonListStyle" TargetType="{x:Type ListBox}">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<RadioButton
Focusable="False"
IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"
>
<ContentPresenter>ContentPresenter>
RadioButton>
ControlTemplate>
Setter.Value>
Setter>
Style>
Setter.Value>
Setter>
Style>
Window.Resources>
<Grid Margin="8">
<Grid.RowDefinitions>
<RowDefinition>RowDefinition>
<RowDefinition Height="Auto">RowDefinition>
Grid.RowDefinitions>
<ListBox Style="{StaticResource RadioButtonListStyle}" Name="lstProducts"
DisplayMemberPath="ModelName">
ListBox>
Grid>
Window>
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace DataBinding
{
///
/// Interaction logic for RadioButtonList.xaml
///
public partial class RadioButtonList : System.Windows.Window
{
public RadioButtonList()
{
InitializeComponent();
lstProducts.ItemsSource = GetProducts();
}
public ICollection<Product> GetProducts()
{
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("GetProducts", con);
cmd.CommandType = CommandType.StoredProcedure;
ObservableCollection<Product> products = new ObservableCollection<Product>();
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
// Create a Product object that wraps the
// current record.
Product product = new Product((string)reader["ModelNumber"],
(string)reader["ModelName"], (decimal)reader["UnitCost"],
(string)reader["Description"], (string)reader["CategoryName"],
(string)reader["ProductImage"]);
// Add to collection
products.Add(product);
}
}
finally
{
con.Close();
}
return products;
}
}
Please make use this code.i have seen your code it's not clear for me. that y i work and give to you
thank you
Ashok PPosted Aug 12, 2009, 2:37 PM
Hi,
I have created list of RadioButtons inside ItemsControl(or ListBox) based on the XML data. I want to set different values (from XML) for each radio button and need to get the corresponding value when user check on any radiobutton.
Please help on this and I have given the code snipet below,
....
<DataTemplate x:Key="DTRB">
<StackPanel Orientation="Horizontal" Height="100">
<RadioButton GroupName="group1" Height="80" FontSize ="20">
<TextBlock Width="410" TextAlignment="Left" HorizontalAlignment="Left" VerticalAlignment="Center"
TextWrapping="Wrap" Text="{Binding XPath=.}" />
RadioButton>
StackPanel>
DataTemplate>
......
....
<ListBox x:Name="List1" Grid.Row="1" Template="{StaticResource OptionTemplate}"
ItemTemplate="{StaticResource DTRB}" />
....
Master BillaPosted Aug 12, 2009, 11:45 AM
I assume you have value to to bind to radio button
For example i have assume you have a field called IsMale =true or false;
Just assign this to radio button.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="500">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.1*" >
RowDefinition>
<RowDefinition Height="*">
RowDefinition>
Grid.RowDefinitions>
<WrapPanel Name="buttonandText" Margin="0">
<RadioButton Name="rdoGender" IsChecked="{Binding IsMale}">RadioButton>
WrapPanel>
Grid>
Window>