0
votes
I have some problem in wpf application.

In XAML:   

     <Expander Header="SomeHeader" Style="{StaticResource ExpanderStyle}" IsExpanded="{Binding ElementName=Errors, Converter={StaticResource visibilityConverter}, Path=IsExpanded}"  >
                <RichTextBox ScrollViewer.VerticalScrollBarVisibility="Visible" Style="{StaticResource RichTextBoxStyle}" Foreground="Red" IsReadOnly="True">
                        <FlowDocument>
                            <Paragraph>
                                <ItemsControl ItemsSource="{Binding Path=Errors}">
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding}" Style="{StaticResource ErrorTextBlockStyle}"/>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                            </Paragraph>
                        </FlowDocument>
                    </RichTextBox>
            </Expander>    
In my ViewModelClass:   

       private List<string> errors;
       public List<string> Errors
       {
            get { return errors; }
            set
            {
                errors = value;
                OnPropertyChanged("Errors");
            }
       }      

In constructor:

       public MainWindowViewModel()
       {    
           if (IsInDesignMode) return;    
           Errors = new List<string>();    
       }    

In test method:

       private void TestExcute()
       {
           Errors = "Some error";    
       }

In this situation error message not displayed in wpf window. But if I change code in constructor to next:

      public MainWindowViewModel()
      {    
           if (IsInDesignMode) return;    
           Errors = new List<string>{"errorMessage1", "errorMessage2"};    
      }

Displayed:

errorMessage1

errorMessage2

What is the reason ?

I have new question. In this wpf application I also used Expander control. How create auto expand open, then Errors.count > 0?

I create converter :

public class VisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { LoadFile loadFile = (LoadFile)value;

        if (loadingFile.ExcelErrors.Count > 0)
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Collapsed;
        }
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

LoadFile is a class where declared Errors property.

1

1 Answers

2
votes

I think you made an error in your TestExcute while writing question did you mean to write Errors.Add("some error")?

If so then your ItemsControl wont react to change because there is no change on property Errors - setter is not invoked.

Change your List<string> to ObservableCollection<string> this class notifies that its content has change and UI will react to that.