最近在做一個工具如下:根據電壓、容值、公差去比對 "K3規格型号”,如果不存在則将 “電壓”、“容值”、“公差”字型顔色改為紅色。
當時遇到了這個問題,因為Column "功率"已經綁定了一個字段 “Power”,想要根據CellColor改變顔色,在cell裡面無法擷取到“CellColor”的資料,這個問題花了一天多時間
才找到答案:Foreground="{Binding RowData.Row.CellColor,Converter={StaticResource ColorChoiceConvert}}" ,綁定了這一行的資料,這樣在轉換器裡就可以擷取到cellColor屬性的資料,做出判斷。
<dxg:GridColumn Header="功率" FieldName="Power" HorizontalHeaderContentAlignment="Center">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
< TextBlock Text="{Binding Path=Value}" Foreground="{Binding RowData.Row.CellColor,Converter={StaticResource ColorChoiceConvert}}"/>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
前端xaml代碼如下:
<UserControl xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
x:Class="LayoutTool.Controls.ucResistanceManage"
xmlns:dxdo="http://schemas.devexpress.com/winfx/2008/xaml/docking"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:entity="clr-namespace:LayoutTool.Entity"
xmlns:convert="clr-namespace:LayoutTool.CustomConvert"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<convert:ColorChoiceConvert x:Key="ColorChoiceConvert"/>
<convert:OhmAndPfColorConvert x:Key="OhmAndPfColorConvert"/>
<convert:ToleranceColorConvert x:Key="ToleranceColorConvert"/>
</UserControl.Resources>
<dxg:GridControl Name="dgResistance" >
<dxg:GridControl.View>
<dxg:TableView Name="tvResistance" AllowPerPixelScrolling="True" AllowEditing="False" ShowTotalSummary="False" ShowGroupPanel="False">
</dxg:TableView>
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn Header="物料編碼" FieldName="Code" HorizontalHeaderContentAlignment="Center"/>
<dxg:GridColumn Header="label資訊" FieldName="LabelMessage" HorizontalHeaderContentAlignment="Center"/>
<dxg:GridColumn Header="功率" FieldName="Power" HorizontalHeaderContentAlignment="Center">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value}" Foreground="{Binding RowData.Row.CellColor,Converter={StaticResource ColorChoiceConvert}}"/>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
<dxg:GridColumn Header="阻值" FieldName="ResistanceValue" HorizontalHeaderContentAlignment="Center">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value}" Foreground="{Binding RowData.Row.CellColor,Converter={StaticResource OhmAndPfColorConvert}}"/>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
<dxg:GridColumn Header="公差" FieldName="Tolerance" HorizontalHeaderContentAlignment="Center">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value}" Foreground="{Binding RowData.Row.CellColor,Converter={StaticResource ToleranceColorConvert}}"/>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
<dxg:GridColumn Header="K3規格型号" FieldName="K3Model" Width="*" HorizontalHeaderContentAlignment="Center"/>
</dxg:GridControl.Columns>
</dxg:GridControl>
(2)上面我用的時GridControl控件,當使用DataGrid控件時,解決上述的問題可以使用如下方法:
<DataGrid x:Name="dgResistance" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="功率"
Width="2*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DataContext,RelativeSource={ RelativeSource AncestorType=DataGridRow},Converter={StaticResource ColorChoiceConvert}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>