繁体   English   中英

使用不同的 ItemSource WPF C# 绑定属性

[英]Binding a property with a different ItemSource WPF C#

我有一个绑定为 ItemSource 的列表,其中包含两个字符串:选项 1 和选项 2,我有 2 个文本框,可以在其中绑定和显示这两个选项。 我在两个文本框旁边还有两个单选按钮。 我想绑定这些单选按钮,但每次单击它们时都没有任何反应。 我找到了原因,因为现在无论按钮是否被选中,他总是试图在我的列表中找到 bool。 有没有办法在 xaml 代码中设置我可以访问我的 ViewModel 中的 bool 属性?

视图模型:

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>

ItemsControl中每个ItemTemplateItemContainerStyle DataContext会自动设置为ItemsSource的相应元素。

DataContext重定向到元素外部某处的一种方法是从Window根对象的DataContext开始绑定路径。

所以,如果你WortAuswahlViewModel设置为DataContext一个的Window ,首先你需要设置绑定源到Window使用RelativeSource={RelativeSource AncestorType=Window} ,然后设置路径, Path=DataContext.CheckButtonEins

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

如果您的WortAuswahlViewModel设置为另一个 UI 元素的DataContext ,请将Window替换为该元素的类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM