Usando Converter con imagen dentro de la cuadrícula de datos

I'm stumbling on this data converter stuff, especially with relation to multiple rows and dynamically show/hide an image.

Lets take a master/detail data perspective. On a given view (presentation), I have header information and a data grid showing a bunch of line items. Before saving the line items, there could be one or more rows having missing/invalid data. I want to display a graphic image to the left of the line item giving the user some visual cue that ...hey, this row needs to be fixed...

So, I have a datatable on my ViewModel that has an extra column indicating if there are errors associated with the row as boolean as datatable column types have no idea how to handle the wpf "visibility" data type. This datatable.DefaultView is the actual basis of displaying the data (which works). I'm just stuck on this final getting the image to dynamically show/hide properly.

I've read about converters, and have a class that does nothing but act as the converter. So, I can only suggest the following for those who can offer help.

My ViewModel has a property exposing the DefaultView

public DataView MyDetailView
{ get { return MyTable.DefaultView; }}

For simplicity, this table has two columns..

RecordIsInvalid (boolean), 
LineItem (int)

preguntado el 08 de noviembre de 11 a las 13:11

1 Respuestas

Using BooleanToVisibilityConverter Binding should be pretty straightforward:

<Image Visibility="{Binding RecordIsInvalid, 
                    Converter={StaticResource BooleanToVisibilityConverter}}" ... />

But as far as I understand you have a single RecordIsInvalid bandera y LineNumber which refer to a specific Row. What actually bound to each Row? Do you have someting like ItemViewModel which correspond to each row? Basically each Item should be in charge to validate it's own state and expose IsValid property in this way things would be much clean and easy so you would be able simply bound to IsValid in scope of each grid view Item.

EDIT: Responder al comentario

You should not instantiate and expose converter yourself.

  • Put a converter class in some adequate namespace like MyProject.GUI.Converters
  • In View.xaml add namespace alias for Converetrs, see *1
  • Add converter into the Control/Window resources within XAML, see *2

*1: MyView.xaml

<UserControl ...
       xmlns:Converters="clr-namespace:MyProject.GUI.Converters" />

*2: MyView.xaml

    <Converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

respondido 08 nov., 11:18

It is NOT handled by a separate view model. It is just a dataview that is exposed on the MAIN view model. As for the converter, I currently have that as a separate class and is NOT in the view model itself. What is the best way to make the converter available... as an instanciated instance of the class, and expose it as a property for the view's DataContext to allow it to be exposed? I'm still learning a bunch of this complex XAML vs ViewModel, static reference, etc binding references. - DRapp

Yup... that did it. I didn't originally understand the context, but your clear example for the single element (as opposed to so many that are super nested) got me directly to the point... Thanks - DRapp

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.