How do I assign Content of a Checked Radio-button in WPF to a property in ViewsModel

Oladapo Kolawole Osagie 21 Reputation points
2022-07-26T18:43:05.877+00:00

I want the Content of Selected RadioButton set to a Property in my viewsmodel224900-xaml.png225011-selectedanswer.png

Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2022-07-27T07:11:25.443+00:00

    Hi,@Oladapo Kolawole Osagie . Welcome Microsoft Q&A.

    For binding the value of the selected RaidoButton to the property, you could try referring to the code below.
    Xaml:

    <Window.Resources>  
            <ObjectDataProvider x:Key="selectOptions" ObjectType="{x:Type local:MyQuestion}" />  
            <local:RadioButtonCheckedConverter x:Key="RadioButtonCheckedConverter"  />  
        </Window.Resources>  
        <StackPanel>  
            <ListView Name="lv"  Background="AliceBlue"  ItemsSource="{Binding AllQuestions}" SelectedItem="{Binding SelectedQuestion}" SelectionChanged="ListView_SelectionChanged">  
                <ListView.ItemTemplate>  
                    <DataTemplate>  
                        <StackPanel Background="LightPink" >  
                            <StackPanel Orientation="Horizontal">  
                                <TextBlock Text="{Binding Id}" FontSize="24"/>  
                                <TextBlock FontSize="20" Text="{Binding Question}"/>  
                            </StackPanel>  
                            <StackPanel Margin="40,0" >  
                                <RadioButton GroupName="{Binding Id}" FontSize="16" Content="{Binding OptionA}"   IsChecked="{Binding SelectedAnswer ,Converter= {StaticResource RadioButtonCheckedConverter}, ConverterParameter={x:Static local:Options.OptionA}}"/>  
                                <RadioButton GroupName="{Binding Id}" FontSize="16" Content="{Binding OptionB}"    IsChecked="{Binding SelectedAnswer ,Converter= {StaticResource RadioButtonCheckedConverter}, ConverterParameter={x:Static local:Options.OptionB}}" />  
                                <RadioButton GroupName="{Binding Id}" FontSize="16" Content="{Binding OptionC}"    IsChecked="{Binding SelectedAnswer ,Converter= {StaticResource RadioButtonCheckedConverter}, ConverterParameter={x:Static local:Options.OptionC}}"/>  
                                <RadioButton GroupName="{Binding Id}" FontSize="16" Content="{Binding OptionD}"   IsChecked="{Binding SelectedAnswer ,Converter= {StaticResource RadioButtonCheckedConverter}, ConverterParameter={x:Static local:Options.OptionD}}"/>  
                            </StackPanel>  
                        </StackPanel>  
                    </DataTemplate>  
                </ListView.ItemTemplate>  
    
            </ListView>  
            <TextBlock x:Name="tb" Text="{Binding ElementName=lv ,Path=SelectedItem.SelectedAnswer , UpdateSourceTrigger=PropertyChanged}" Height="50" Background="LightGreen" />  
        </StackPanel>  
    

    Codebehind: You can click on the .txt file to view the code.

    225176-6.txt

    The result:
    225154-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.