WPF GridControl 数据绑定

2017-04-20 20:23:12来源:CSDN作者:wxstar8人点击

最近在做一个工具如下:根据电压、容值、公差去匹配 "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>


   

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台