locked
get Value from RadioButton in DataGrid RRS feed

  • Question

  • Hello,

    I'm working on my first WPF project.

    I'm populating a DataGrid with Values from a MySQL query.

    The XAML for the DataGrid:

    <DataGrid x:Name="DataGridDatabases" ItemsSource="{Binding ViewModelDatabase}" AutoGenerateColumns="False" CanUserAddRows="False" Height="330" Width="350" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0" BorderBrush="Black" AlternatingRowBackground="LightGray" IsReadOnly="True">
                        <DataGrid.Columns >
                            <DataGridTemplateColumn Header="" Width="25">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <RadioButton GroupName="Database" IsChecked="{Binding IsSelected}" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>

                            <DataGridTextColumn Header="Database" Binding="{Binding Name}" Width="230"/>
                            <DataGridTextColumn Header="Size (MB)" Binding="{Binding Size}" Width="*"/>
                        </DataGrid.Columns>
                        <DataGrid.DataContext>
                            <Models:PCDatabase/>
                        </DataGrid.DataContext>
     </DataGrid>

    As you can see, there's a RadioButton in the first column of my DataGrid. This Button is there to visualize which Database is currently selected

    Now I want to create another button that reacts on depending which RadioButton in the DataGrid is selected. How can I achieve this?

    Wednesday, October 7, 2015 11:05 AM

Answers

  • Oh.. you mean checked rather than selected.

    You could put your logic in the setter for the Boolean property your radio button ischecked is bound to.

    public bool IsChecked
    {
        get
        {
            return isChecked;
        }
        set
        {
            isChecked= value;
            RaisePropertyChanged();
            DoSomethingWhenCheckedChanged();
        }
    }

    And do whatever your logic is for this button in DoSomethingWhenCheckedChanged.

    .

    By the way.

    You could potentially obviate the radiobutton completely and use the selected row.

    When you click on a row to edit the radio button, you will be selecting it.

    (Although you obviously do not have to click on that radiobutton in a row once selected ).


    Hope that helps.

    Technet articles: WPF: MVVM Step 1; All my Technet Articles

    Thursday, October 8, 2015 10:15 AM
  • If you want to synchronize the selection of your RadioButton with the selection of a row in the DataGrid, you could define an ItemContainerStyle for the DataGrid and bind the IsSelected property of the DataGridRow to your IsSelected source property:

            <DataGrid ... >
                <DataGrid.ItemContainerStyle>
                    <Style TargetType="DataGridRow">
                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                    </Style>
                </DataGrid.ItemContainerStyle>
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="" Width="25">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <RadioButton GroupName="Database" IsChecked="{Binding IsSelected}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
    
                </DataGrid.Columns>
            </DataGrid>
    

    Note that your data object class should implement the INotifyPropertyChanged interface and raise the PropertyChanged interface whenever the IsSelected property is set, e.g.:

            public class MyClass : INotifyPropertyChanged
            {
                private bool _isSelected;
    
                public bool IsSelected
                {
                    get { return _isSelected; }
                    set { _isSelected = value; NotifyPropertyChanged(); }
                }
    
    
                public event PropertyChangedEventHandler PropertyChanged;
                private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] String propertyName = "")
                {
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                    }
                }
            }

    But please try to be more specific about your issue when starting a new thread.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Thursday, October 8, 2015 3:41 PM

All replies

  • Determining which row that is currently selected is the same thing as finding the first (and only) row where the IsSelected source property is set to true:

    PCDatabase db = DataGridDatabases.DataContext as PCDatabase;
    YourType selectedObject = db.ViewModelDatabase.FirstOrDefault(x => x.IsSelected == true);

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Wednesday, October 7, 2015 11:56 AM
  • I don't follow the

    "

    Now I want to create another button that reacts on depending which RadioButton in the DataGrid is selected. How can I achieve this?

    "

    It depends what you want to do. And I'm not sure how much wpf and MVVM you know. But you seem to be using mvvm there.

    Personally I would use this approach here:

    http://social.technet.microsoft.com/wiki/contents/articles/30564.wpf-uneventful-mvvm.aspx#Select_From_List_IndexChanged

    And bind SelectedItem. You can do stuff in the setter as which one is selected changes:

    public Person SelectedChef
    {
        get
        {
            return selectedChef;
        }
        set
        {
            selectedChef = value;
            RaisePropertyChanged();
            DoSomethingWhenChefChanged();
        }
    }

    (The sample code has a collection of Chefs bound).

    You would possibly bind some property of this button you're talking about and change that to do whatever you mean by "reacts" in your equivalent to DoSomethingWhenChefChanged();


    Hope that helps.

    Technet articles: WPF: MVVM Step 1; All my Technet Articles


    • Edited by Andy ONeill Wednesday, October 7, 2015 12:13 PM
    • Proposed as answer by Xavier Xie-MSFT Thursday, October 8, 2015 8:14 AM
    • Unproposed as answer by Xavier Xie-MSFT Wednesday, October 21, 2015 9:35 AM
    Wednesday, October 7, 2015 12:13 PM
  • But that's the thing: I don't want to care about which row is currently selected, I want to make my choice of the "active" row by clicking the radio button. But there's the possibilty of the radio button being checked in one row and another row being selected.
    Thursday, October 8, 2015 9:17 AM
  • Oh.. you mean checked rather than selected.

    You could put your logic in the setter for the Boolean property your radio button ischecked is bound to.

    public bool IsChecked
    {
        get
        {
            return isChecked;
        }
        set
        {
            isChecked= value;
            RaisePropertyChanged();
            DoSomethingWhenCheckedChanged();
        }
    }

    And do whatever your logic is for this button in DoSomethingWhenCheckedChanged.

    .

    By the way.

    You could potentially obviate the radiobutton completely and use the selected row.

    When you click on a row to edit the radio button, you will be selecting it.

    (Although you obviously do not have to click on that radiobutton in a row once selected ).


    Hope that helps.

    Technet articles: WPF: MVVM Step 1; All my Technet Articles

    Thursday, October 8, 2015 10:15 AM
  • If you want to synchronize the selection of your RadioButton with the selection of a row in the DataGrid, you could define an ItemContainerStyle for the DataGrid and bind the IsSelected property of the DataGridRow to your IsSelected source property:

            <DataGrid ... >
                <DataGrid.ItemContainerStyle>
                    <Style TargetType="DataGridRow">
                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                    </Style>
                </DataGrid.ItemContainerStyle>
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="" Width="25">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <RadioButton GroupName="Database" IsChecked="{Binding IsSelected}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
    
                </DataGrid.Columns>
            </DataGrid>
    

    Note that your data object class should implement the INotifyPropertyChanged interface and raise the PropertyChanged interface whenever the IsSelected property is set, e.g.:

            public class MyClass : INotifyPropertyChanged
            {
                private bool _isSelected;
    
                public bool IsSelected
                {
                    get { return _isSelected; }
                    set { _isSelected = value; NotifyPropertyChanged(); }
                }
    
    
                public event PropertyChangedEventHandler PropertyChanged;
                private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] String propertyName = "")
                {
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                    }
                }
            }

    But please try to be more specific about your issue when starting a new thread.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Thursday, October 8, 2015 3:41 PM