在数据网格中使用转换器与图像 [英] Using Converter with image within datagrid

查看:135
本文介绍了在数据网格中使用转换器与图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绊倒了这个数据转换器的东西,特别是与多行关系,动态显示/隐藏图像。

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

所以,我的ViewModel上有一个datatable,它有一个额外的列,指示是否有与行相关联的错误为布尔值,因为datatable列类型不知道如何处理wpfvisibility数据类型。这个datatable.DefaultView是显示数据的实际基础(它的工作原理)。我只是坚持这个最终得到的图像来动态地显示/隐藏。

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.

我的ViewModel有一个暴露DefaultView的属性

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)


推荐答案

使用BooleanToVisibilityConverter绑定应该很简单:

Using BooleanToVisibilityConverter Binding should be pretty straightforward:

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

但据我所知,您有一个 RecordIsInvalid 标志和 LineNumber ,它指的是一个特定的行。什么实际绑定到每一行?你有像ItemViewModel那样对应于每一行吗?基本上每个项目应该负责验证它自己的状态,并以这种方式暴露 IsValid 属性,这将是非常干净和容易的,所以你可以简单地绑定到 IsValid 在每个网格视图项目的范围内。

But as far as I understand you have a single RecordIsInvalid flag and 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.

编辑:回覆评论

您不应该自己实例化和公开转换器。

You should not instantiate and expose converter yourself.


  • 将转换器类置于一些适当的命名空间像 MyProject.GUI.Converters

  • 在View.xaml中为Converetrs添加命名空间别名,请参阅* 1

  • 将转换器添加到XAML中的控制/窗口资源中,请参阅* 2

* 1: MyView.xaml

*1: MyView.xaml

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

* 2: MyView.xaml

*2: MyView.xaml

<UserControl.Resources>
    <Converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</UserControl.Resources>

这篇关于在数据网格中使用转换器与图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆