locked
binding error simple obsevalcollection to radiobutton RRS feed

  • Question

  • xaml code

    <GridView Grid.Column="1" Name="param1X" ItemsSource="{Binding Path=radioButtonContent1}">
                            <GridView.ItemTemplate>
                                <DataTemplate>
                               <RadioButton Content="{Binding Path=radioButtonContent1}" GroupName="1"  IsChecked="True"/>
                                </DataTemplate>
                            </GridView.ItemTemplate>
                        </GridView>

    c# code

     ObservableCollection<string> radioButtonContent1 { get; set; }
            public socialDataReports()
            {
                this.DataContext = this;
                radioButtonContent1 = new ObservableCollection<string>();
                this.InitializeComponent();
                colorreceived();
                radioButtonContent1.Add("Sales Office");
                radioButtonContent1.Add("Sales Group");
                radioButtonContent1.Add("Customer Name"); 
            }

    what am i doing error the value is not viewable

    if i put this code then i see  
                 param1X.ItemsSource = radioButtonContent1;

      then its binding properly but all radio button are checked how to rectify this

    • Edited by prasanas Monday, October 7, 2013 6:39 AM
    Monday, October 7, 2013 6:25 AM

Answers

  • Hi,prasanas

    Welcom to Windows store app forum! About your problem, there is a sample below you can refer to:

    In XAML:

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <GridView  Name="param1X" ItemsSource="{Binding}">
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <RadioButton Content="{Binding Content}" GroupName="1" Width="200"/>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
        </Grid>

    Code-Behind:

    namespace GridViewVisual
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            ObservableCollection<RaioContent> radioButtonContent1;
            public MainPage()
            {
                this.InitializeComponent();
                radioButtonContent1 = new ObservableCollection<RaioContent>();
                radioButtonContent1.Add(new RaioContent { Content = "Sales Office" });
                radioButtonContent1.Add(new RaioContent { Content = "Sales Group" });
                radioButtonContent1.Add(new RaioContent { Content = "Customer Name" });
                param1X.DataContext = radioButtonContent1;
            }
    
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }
        }
    
        public class RaioContent
        {
            public string Content { get; set; }
        }
    }
    

    And I suggest you can see these links below to get more information about how to Bind data to controls:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh464965.aspx

    http://msdn.microsoft.com/en-us/library/windows/apps/hh758320.aspx

    Also,there is a simple in MSDN you can see:

    http://code.msdn.microsoft.com/windowsapps/XAML-data-binding-sample-8e845a6f

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by prasanas Tuesday, October 8, 2013 2:13 PM
    Tuesday, October 8, 2013 2:24 AM

All replies

  • 1. Implement interface INotifyPropertyChanged.

    2. Don't initialize collection in constructor, use page.load / navigation event handlers instead. 

    3.  but all radio button are checked how to rectify this

    delete IsChecked="True" from DataTemplate

    Monday, October 7, 2013 7:12 AM
  • Hi oleg,

      i m new to c# i cant understand 2 and 3 can you post sample please

    Monday, October 7, 2013 9:12 AM
  • Hi,prasanas

    Welcom to Windows store app forum! About your problem, there is a sample below you can refer to:

    In XAML:

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <GridView  Name="param1X" ItemsSource="{Binding}">
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <RadioButton Content="{Binding Content}" GroupName="1" Width="200"/>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
        </Grid>

    Code-Behind:

    namespace GridViewVisual
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            ObservableCollection<RaioContent> radioButtonContent1;
            public MainPage()
            {
                this.InitializeComponent();
                radioButtonContent1 = new ObservableCollection<RaioContent>();
                radioButtonContent1.Add(new RaioContent { Content = "Sales Office" });
                radioButtonContent1.Add(new RaioContent { Content = "Sales Group" });
                radioButtonContent1.Add(new RaioContent { Content = "Customer Name" });
                param1X.DataContext = radioButtonContent1;
            }
    
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }
        }
    
        public class RaioContent
        {
            public string Content { get; set; }
        }
    }
    

    And I suggest you can see these links below to get more information about how to Bind data to controls:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh464965.aspx

    http://msdn.microsoft.com/en-us/library/windows/apps/hh758320.aspx

    Also,there is a simple in MSDN you can see:

    http://code.msdn.microsoft.com/windowsapps/XAML-data-binding-sample-8e845a6f

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by prasanas Tuesday, October 8, 2013 2:13 PM
    Tuesday, October 8, 2013 2:24 AM