简体   繁体   中英

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?

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. 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?

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

<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. It still shows up in the bottom since I didn't put any checking for validation there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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