简体   繁体   English

数据绑定-重用列表,但选择的项目不同

[英]Databinding — Reusing list, but different selecteditems

I've got a datagrid with a structure like this: 我有一个具有以下结构的数据网格:

Question 1  text question 1    <pull down>
Question 2  text question 2    <pull down>
Question 3  text question 3    <pull down>

The pull down comboboxes all have the same options: Yes, No, Maybe. 下拉组合框均具有相同的选项:是,否,也许。 Right now, I put the options in an observablecollection. 现在,我将这些选项放在一个可观察的集合中。

My question is: How do I bind the selected item property of the combobox so that it pulls from a different object than the source observablecollection? 我的问题是:如何绑定组合框的选定项属性,以便它从与源observablecollection不同的对象中提取?

Here's some code that might make it clearer what I'm trying to do 这是一些可以使我更清楚地知道要做什么的代码

   public class ViewModel
   {
       ObservableCollection<string> options;
       ObservableCollection<question> questions;
   }

   public class question
   {
       public string selectedOption;
   }

If I understand your question properly, I think the short answer is "you can't". 如果我正确理解了您的问题,我认为简短的答案是“您不能”。 WPF checks to see if the selecteditem is part of the itemssource and will not display the selecteditem if it is not. WPF检查所选项目是否是itemssource的一部分,如果不是,则不会显示所选项目。 What kind of behavior are you trying to create for your user? 您试图为用户创建什么样的行为? Perhaps there's another way to accomplish what you want. 也许还有另一种方法可以实现您想要的。

edit Perhaps this similar/duplicate question + answer will help you as well? 编辑也许这个类似/重复的问题+答案也会对您有所帮助? How to set SelectedItem of a ComboBox when item is not part of the ItemsSource collection? 当item不属于ItemsSource集合时,如何设置ComboBox的SelectedItem?

If I'm interpreting your question correctly, you want to use the same source list for the answer options to the questions, but want to make sure that the selections remain independent of each other. 如果我正确地解释了您的问题,那么您希望对问题的答案选项使用相同的来源列表,但要确保选择之间保持独立。 Is something like this what you're looking for? 您正在寻找类似这样的东西吗?

XAML XAML

<StackPanel>
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Content="Question 1"/>
            <ComboBox x:Name="cbxOne" ItemsSource="{Binding Options}"
                        SelectedItem="{Binding Questions[0].selectedOption}"></ComboBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Content="Question 2"/>
            <ComboBox ItemsSource="{Binding Options}"
                      SelectedItem="{Binding Questions[1].selectedOption}"></ComboBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Content="Question 2"/>
            <ComboBox ItemsSource="{Binding Options}"
                      SelectedItem="{Binding Questions[2].selectedOption}"></ComboBox>
        </StackPanel>
    </StackPanel>
    <ListBox ItemsSource="{Binding Questions}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Label Content ="{Binding selectedOption}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</StackPanel>

Codebehind 代码隐藏

public partial class MainWindow : Window
{

    ViewModel vm;
    public MainWindow()
    {
        InitializeComponent();
        vm = new ViewModel();
        this.DataContext = vm;
    }


}
public class ViewModel
{
    public ObservableCollection<string> Options { get; set; }
    public ObservableCollection<question> Questions { get; set; }
    public ViewModel()
    {
        Options = new ObservableCollection<string> { "Yes", "No", "Maybe" };
        Questions = new ObservableCollection<question>();
        Questions.Add(new question());
        Questions.Add(new question());
        Questions.Add(new question());
    }

}

public class question
{
    public string selectedOption { get; set; }
}

捕获展示绑定作品

If in the test constructor there I assign a value to questions in the collection like such 如果在测试构造函数中,我为类似这样的集合中的问题分配一个值

Questions[0].selectedOption = "Yes";
Questions[1].selectedOption = "bogus";

Then the first combobox will gave "Yes" already selected because "Yes" exists in the collection used as the ItemSource, but the second will be blank because there is no matching value. 然后,第一个组合框将给出已经选择的“是”,因为在用作ItemSource的集合中存在“是”,但是第二个组合框将为空白,因为没有匹配值。 It still shows up in the bottom since I didn't put any checking for validation there. 它仍然显示在底部,因为我没有在此处进行任何检查验证。

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

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