views:

30

answers:

1

I hope that this question has not been asked elsewhere, but I'm having difficulty finding the right search terms to bring up any already-existing answers to this problem.

I'm implementing a program in C# WPF (MVVM) with many interview-style screens where the user inputs scientific data in an orderly manner. We would like to have the Textbox's and DataGrid's change Foreground and Background colors on an individual basis based on whether the data in that control has been inputted by the user, inputted as a default value by the program, or is a template value from another file the user imported. On top of this, we would like the UI to respond to validation checks from IDataErrorInfo implemented in the ViewModel.

Thus, the data displayed in a TextBox could be blue if it is a templated value, green if a program default, black if user inputed, and red if IDataErrorInfo says it is bad data.

My initial answer for implementing this was to create a custom class:

class AdornerString{

private string _myString;
private bool _isTemplate;
private bool _isDefault;

public string MyString{ 
  get{
      etc.
  }
  set{
      etc.
  }
}
// accessor properties and Constructors omitted for brevity
}

Then I have all my TextBox.Text properties in the View bound like so:

<TextBox Text="{Binding Path=someAdornerString.MyString,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"/>

and apply a style with DataTriggers that responds to someAdornerString's properties to create the appropriate colors.

However, IDataErrorInfo on the ViewModel doesn't validate the TextBox anymore. Why is this so, and is there a better way to solve my problem? I can't validate inside the AdornerString class.

A possible work-around, though undesirable:

The only other solution I can think of is to have two properties in the ViewModel for each field entered by the user- one for the data itself and the other being the same custom class as above minus the string. However, this means I can't generalize the style used for the textboxes. Each TextBox would have to have a custom style something like this:

<TextBox.Style>
    <Style TargetType="{x:Type TextBox}"
           BasedOn="{StaticResource OtherStyle}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=**instanceCustomClass**.IsDefault}"
                         Value="True">
                <Setter Property="Foreground"
                        Value="Green" />
            </DataTrigger>
            <Trigger Property="Validation.HasError"
                     Value="true">
                <Setter Property="Foreground"
                        Value="OrangeRed" />
            </Trigger>
        </Style.Triggers>
    </Style>

since each UI field has a specific custom class associated with it.

I would seriously rather not implement this way, as I have possibly 100+ pages of input screens, with each screen having 1-12 TextBox's each, ignoring the DataGrids thrown in the mix (with parellel arrays of data and their associated custom classes).

Any ideas? I've read about custom validation, though I yet don't see how this might help in this case.

A: 

Ignore making use of the IDataErrInfo validation all together, as it seems that the you really want to make it 1 of 4 values...and 'bad' data just happens to be one of them.

You need to keep the items on an even playing field since you are treating them the same, just differentiating colors. Use a single property with an object wrapping the value and the state of the model within the ViewModel. Ignore IDataErroInfo and then use a converter to provide the coloring and then add a delegate to the AdornerString that will be set to the validation function written in the ViewModel for it.

Aaron