将属性与其他ItemSource WPF C#绑定

问题描述

我有一个绑定为ItemSource的列表,其中包含两个字符串:选项1和选项2,我有2个文本框,在其中绑定并显示这两个选项。在两个文本框旁边,我还有两个单选按钮。我想绑定这些单选按钮,但是每次单击它们都不会发生。我找出了原因,因为现在无论是否选中按钮,他总是试图在我的列表中查找布尔值。是否可以在xaml代码中设置可以访问ViewModel中的bool属性的方法?

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" 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" x:Name="TXT_optionZweiLoesung" Text="{Binding OptionZwei}" IsReadOnly="True"/>
                            </DockPanel>
                        </StackPanel>
                    </Viewbox>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</Grid>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)