locked
first radiobutton checked in binding RRS feed

  • Question

  • My XAml Code

     <GridView Grid.Column="1" Name="param1X" ItemsSource="{Binding Path=radioButtonContent1}" Height="120">
                            <GridView.ItemTemplate>
                                <DataTemplate>
                                    <RadioButton Width="150" Content="{Binding}" GroupName="1" />
                                </DataTemplate>
                            </GridView.ItemTemplate>
                        </GridView>

    c# code

    ObservableCollection<string> radioButtonContent1 = new ObservableCollection<string>();

    radioButtonContent1.Add("Tag");
     radioButtonContent1.Add("Category");
    radioButtonContent1.Add("Image");

    param1X.ItemsSource = radioButtonContent1;

    binding all are correct but how to automatically set first (tag example) as checked in loading


    • Edited by prasanas Monday, October 7, 2013 10:11 AM
    Monday, October 7, 2013 10:11 AM

Answers

  • Create new class:

    public class MyClass : INotifyPropertyChanged
        {
            private string isChecked;
    
            public string IsChecked
            {
                get { return isChecked; }
                set
                {
                    isChecked = value;
                    this.RaisePropertyChanged();
                }
            }
    
            private string text;
    
            public string Text
            {
                get { return text; }
                set
                {
                    text = value;
                    this.RaisePropertyChanged();
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void RaisePropertyChanged([CallerMemberName] string propertyName = "")
            {
                var handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }


    Create collection in code:

    ObservableCollection<MyClass> radioButtonContent1 = new ObservableCollection<MyClass>();

    radioButtonContent1.Add(new MyClass {Text = "Tag", IsChecked = true});

    in xaml change datatemplate:

    <DataTemplate>
                                    <RadioButton Width="150" Content="{Binding}" GroupName="1" IsChecked="{Binding IsChecked}" />
                                </DataTemplate>

    Monday, October 7, 2013 10:25 AM