0

I have a list bound as ItemSource that contains two strings: Option 1 and option 2, I have 2 text boxes where I bind and display these two options. I also have two radio buttons next to the two textboxes. I want to bind these radiobuttons but every time I click on them nothing happens. I found out the reason for this, because now he is always trying to find the bool in my list whether the button is checked or not. Is there a way to set in the xaml code that I can access the bool property which is in my ViewModel?

ViewModel:

public class WortAuswahlViewModel : AntwortMoeglichkeitViewModel, IWortAuswahlViewModel
{
    public ObservableCollection<AuswahlOptionen> m_auswahlOptionen;
    public WortAuswahlViewModel(WortAuswahl wortAuswahl)
    {
        if (wortAuswahl?.Optionen == null)
        {
            return; 
        }
        m_auswahlOptionen = new ObservableCollection<AuswahlOptionen>(wortAuswahl.Optionen); 
    }

    public ObservableCollection<AuswahlOptionen> WortAuswahlOptionen
    {
        get
        {
            return m_auswahlOptionen;
        }
        set
        {
            if (m_auswahlOptionen != value)
            {
                m_auswahlOptionen = value;
                OnPropertyChanged();
            }
        }
    }

    private bool m_isRadioButtonCheckedFirst; 
    public bool CheckButtonEins
    {
        get
        {
            return m_isRadioButtonCheckedFirst;
        }
        set
        {
            if (m_isRadioButtonCheckedFirst != value)
            {
                m_isRadioButtonCheckedFirst = value;
                OnPropertyChanged();
            }
        }
    }

    private bool m_isRadioButtonCheckedSecond;
    public bool CheckButtonZwei
    {
        get
        {
            return m_isRadioButtonCheckedSecond;
        }
        set
        {
            if (m_isRadioButtonCheckedSecond != value)
            {
                m_isRadioButtonCheckedSecond = value;
                OnPropertyChanged();
            }
        }
    }
}

}

XAML:

 <Grid>
    <StackPanel Grid.Row="1" Grid.Column="1" Margin="20">
        <ItemsControl ItemsSource="{Binding WortAuswahlOptionen}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Viewbox Height="80" HorizontalAlignment="Left" VerticalAlignment="Top">
                        <StackPanel>
                            <RadioButton HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" IsChecked="{Binding CheckButtonEins}"/>
                            <DockPanel  LastChildFill="True">
                                <TextBox Grid.Column="1" Margin="20, 0, 0, 0" x:Name="TXT_optionEinsLoesung" Text="{Binding OptionEins}" IsReadOnly="True"/>
                            </DockPanel>
                                <RadioButton HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" IsChecked ="{Binding CheckeButtonZwei}"/>
                                <DockPanel  LastChildFill="True">
                                <TextBox Grid.Column="1" Margin="20, 0, 0, 0" x:Name="TXT_optionZweiLoesung" Text="{Binding OptionZwei}" IsReadOnly="True"/>
                            </DockPanel>
                        </StackPanel>
                    </Viewbox>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</Grid>
sim
  • 15
  • 5
  • Does this answer your question? [Access parent DataContext from DataTemplate](https://stackoverflow.com/questions/3404707/access-parent-datacontext-from-datatemplate) – StayOnTarget Sep 09 '20 at 19:00

1 Answers1

0

DataContext of each ItemTemplate and ItemContainerStyle inside ItemsControl is automatically set to the corresponding element of the ItemsSource.

One way to redirect the DataContext to somewhere outside of the elements is to start the binding path from the DataContext of the root object of your Window.

So if your WortAuswahlViewModel is set to the DataContext of a Window, first you need to set the binding source to the Window using RelativeSource={RelativeSource AncestorType=Window} and then set path to Path=DataContext.CheckButtonEins

IsChecked="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.CheckButtonEins}"

If your WortAuswahlViewModel is set to the DataContext of another UI element, replace Window with the type of that element.

Bizhan
  • 16,157
  • 9
  • 63
  • 101
  • Thanks for your answere. This sadly doesnt work i tried something similar. Maybe its helpful to mention that my View is an UserControl? So maybe i need to set a different AncestorType? – sim Sep 04 '20 at 11:28
  • I solved the problem. The Code worked. I just had to set the AncestorType to "UserControl" now it perfectly worked. Thanks for youre help! – sim Sep 04 '20 at 11:41