将Datatemplate应用于Datagrid自动生成的列-列名称的动态绑定

时间:2019-04-05 08:27:02

标签: wpf vb.net data-binding datagrid

我使用了自动生成为true的Datagrid列的Datatemplate,我想动态绑定Datatemplate中的Name列,这样我就可以对所有列使用一个Datatemplate吗?

我尝试使用DataGridTemplateColumn和DataTemplate,但是看起来我必须为每个列创建DataGridTemplateColumn,因此请继续使用DataTemplate。

使用DataTemplate的目的是根据列值显示图像。

XAML:

<Image Name="theImage" Width="40" Height="30" Source="../Resources/Help.png"/>
   <DataTemplate.Triggers>
    <DataTrigger Binding="{Binding Path=Col1}" Value="0,00">
       <Setter TargetName="theImage" Property="Source" Value="../Resources/pmIcons/minus-256.png"/>
    <DataTrigger Binding="{Binding Path=Col1}" Value="1,00">
        <Setter TargetName="theImage" Property="Source" Value="../Resources/pmIcons/greenRoundTick.png"/>
    </DataTrigger>
     <DataTrigger Binding="{Binding Path=Col1}" Value="2,00">
         <Setter TargetName="theImage" Property="Source" Value="../Resources/pmIcons/redCross.png"/>
     </DataTrigger>     
   </DataTemplate.Triggers>
 </DataTemplate>


<DataTemplate>
<Image Name="theImage" Width="40" Height="30" Source="../Resources/Help.png"/>
   <DataTemplate.Triggers>
    <DataTrigger Binding="{Binding Path=Col2}" Value="0,00">
       <Setter TargetName="theImage" Property="Source" Value="../Resources/pmIcons/minus-256.png"/>
    <DataTrigger Binding="{Binding Path=Col2}" Value="1,00">
        <Setter TargetName="theImage" Property="Source" Value="../Resources/pmIcons/greenRoundTick.png"/>
    </DataTrigger>
     <DataTrigger Binding="{Binding Path=Col2}" Value="2,00">
         <Setter TargetName="theImage" Property="Source" Value="../Resources/pmIcons/redCross.png"/>
     </DataTrigger>   
   </DataTemplate.Triggers>
 </DataTemplate>

Pm.xaml.vb:

Private Sub AutoGeneratingColumn(sender As Object, e As DataGridAutoGeneratingColumnEventArgs)


        If e.PropertyName = "IsReadable" Or e.PropertyName = "HasValue" Then
            e.Cancel = True
            Exit Sub
        End If


      Dim oGrdTemplate As DataGridTemplateColumn = New DataGridTemplateColumn
        oGrdTemplate.Header = e.Column.Header               
        oGrdTemplate.CellTemplate = CType(grdTotal.FindResource("test"), DataTemplate)
        e.Column = oGrdTemplate

预期:上面的代码有效,但是我有40列,因此我必须将同一DataTemplate复制40次。我觉得这不是更好的方法。我想在每个列中将相应的列名称绑定为Col1,Col2,Col3等。是否有一种方法可以很好地绑定到Xaml中,如果没有,则可以很好地绑定在代码中?

我尝试过<DataTrigger Binding="{Binding Path=.}" Value="0,00">无效。

2 个答案:

答案 0 :(得分:0)

您的设计还有很大的改进空间,例如,您应该以{{1​​}}作为列,而不是40个不同的命名对象。

但是即使使用当前的设计,您也可以实现Enumeration,将值直接转换为正确的图像URI,然后可以通过转换器将IValueConverter直接绑定到源值。< / p>

未经测试就离开我的头顶

写一个转换器:

Image

嵌入您的资源(<ValueConversion(GetType(Double), GetType(String))> Public Class MyValueToImageConverter Implements IValueConverter Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert Try If CDbl(value) = 0.0 Then Return "../Resources/pmIcons/minus-256.png" If CDbl(value) = 1.0 Then Return "../Resources/pmIcons/greenRoundTick.png" If CDbl(value) = 2.0 Then Return "../Resources/pmIcons/redCross.png" Catch ex As Exception 'this is okay for our purpose End Try Return "../Resources/Help.png" End Function Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack Throw New NotImplementedException() End Function End Class Window等)

Page

并像这样绑定您的列:

<Window.Resources>
    <local:MyValueToImageConverter x:Key="myConverter"/>
</Window.Resources>

然后您可以使用不祥的“标记” <Image Source="{Binding Col1,Converter={StaticResource myConverter}}" />

将此功能包装到UserControl
DependencyProperty

最后得到的是这样干净整洁的东西:

<UserControl
    x:Class="StatusIcon"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d"
    >
    <UserControl.Resources>
        <local:MyValueToImageSourceConverter x:Key="_conv" />
    </UserControl.Resources>
    <Image Source="{Binding Tag, RelativeSource={RelativeSource AncestorType={x:Type local:StatusIcon}}, Converter={StaticResource _conv}}" />
</UserControl>

然后,您可以通过用自己的名为{Value或类似名称的<local:StatusIcon Tag="{Binding Col1}"/> 替换Tag来进一步改进此功能

答案 1 :(得分:0)

我已经提到了@themightylc的答案  我添加了类cls_PMImageConverter, <local:cls_PMImageConverter x:Key="PMImageConverter"/>, <DataTemplate x:Key="colTemplate_Col1"> <Image Source="{Binding Path=Col1, Converter={StaticResource PMImageConverter}}" /> </DataTemplate> <DataTemplate x:Key="colTemplate_Col2"> <Image Source="{Binding Path=Col2, Converter={StaticResource PMImageConverter}}" /> </DataTemplate>

等到col40

,并在AutogenicColumn事件中将此模板称为

Dim oGrdTemplate As DataGridTemplateColumn = New DataGridTemplateColumn
 oGrdTemplate.Header = e.Column.Header
 oGrdTemplate.CellTemplate = CType(grdTotal.FindResource("colTemplate_"+e.PropertyName), DataTemplate)
 e.Column = oGrdTemplate `

它有效..但是,它不是动态仪式吗?我可以进一步改进此代码吗?