Does Filering an ObservableCollection allows edition of entries?

Answered Does Filering an ObservableCollection allows edition of entries?

  • sábado, 14 de abril de 2012 11:51
     
      Contém Código

    Dear all,

    I have an ObervableCollection<Question> type which contains a set of a question list named QuestionList.
    Each of those question are bind to its own text box in order to allow edition of the questions.
    They actually bind to the Title property of the Question object

    <TextBox.Text>
      <Binding Path="QuestionList[0].Title" UpdateSourceTrigger="LostFocus" >
        <Binding.ValidationRules>
         <validators:IsValidCaractersRules />
        </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>

    From the code above the QuestionList is a Notifyied property. So I have this binding type for each question. This works ok and from this I can modify the question Title if needed.

    Each question belongs to a categorie which is a property of the Question Type. What I need to do is having the possibility to filter the question by categorie in order that I can have a better view for editing them.

    How can I filtler my QuestionList in order that I get list of question only from a selected categorie and still beeing able to edit the filter question which should be updated back in the QuestionList ?

    And how the binding behave then, should I bind to the fileter collection or still the QuestionList ?

    regards

    serge


    Your knowledge is enhanced by that of others.

Todas as Respostas

  • sábado, 14 de abril de 2012 14:01
     
     Respondido Contém Código

    Hi,

    i think you should create a new filteredquestion property in your code behind or view model (and bind to that in your view). The property will not be a new list of questions but the Questionslist property (filtered).

    like this -

    public ObservableCollection<Question> QuestionList { get; set; }
    
            public string SelectedCategory { get; set; }
            public ObservableCollection<Question> filteredQuestions
            {
                get 
                {
                    if (string.IsNullOrEmpty(SelectedCategory))
                        return (ObservableCollection<Question>)filteredQuestions.Where(question => question.Category == SelectedCategory);
                    else
                        return QuestionList;
                }
            }

    Do you think that a good solution to your problem?

  • sábado, 14 de abril de 2012 16:39
     
     

    Strange how sometimes ideas gets common before the aswer :-)

    this is exactly what I have done but was thinking earlier if any more elegant way.

    Thnaks

    serge


    Your knowledge is enhanced by that of others.