在DataGrid中拟合图片

时间:2017-08-04 14:09:32

标签: c# wpf datagrid

我正在尝试在我的DataGrid中放置一张图片。所以我使用了DataGridTemplateColumn,但结果是“毛病”。

这是我的wpf提取代码:

<DataGrid MinRowHeight="24" x:Name="ListFamily" Grid.Column="1" 
                  Margin="2" VerticalContentAlignment="Stretch" 
                  HorizontalContentAlignment="Stretch" 
                  AutoGenerateColumns="False" IsReadOnly="True" 
                  SelectionChanged="ListFamily_SelectionChanged" 
                  CanUserReorderColumns="False"
                  CanUserSortColumns="False" Grid.Row="1">
    <DataGrid.Columns>
        <!-- some datagridcolumn -->
        <DataGridTemplateColumn Width="Auto" CanUserResize="False" IsReadOnly="True">
           <DataGridTemplateColumn.Header>
               <Label Content="{x:Static res:StringResources.language}" ToolTip="{x:Static tt:TooltipStrings.language}" HorizontalContentAlignment="Stretch"/>
           </DataGridTemplateColumn.Header>
           <DataGridTemplateColumn.CellTemplate>
               <DataTemplate>
                  <Image Source="{Binding Language, Converter={StaticResource StringToPicture}}" Height="{Binding RowHeight, ElementName=ListFamily}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Uniform"/>
               </DataTemplate>
           </DataGridTemplateColumn.CellTemplate>
           <DataGridTemplateColumn.CellStyle>
               <Style>
                   <Setter Property="FrameworkElement.VerticalAlignment" Value="Center"/>
               </Style>
             </DataGridTemplateColumn.CellStyle>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

这是我得到的:

Picture size is glitching ?

我试图绑定DataGrid的RowHeight,但没有运气。有什么想法吗?

由于

蒂博

1 个答案:

答案 0 :(得分:0)

使用此代码正在运行:

<DataGridTemplateColumn Width="Auto" CanUserResize="False" IsReadOnly="True">
   <DataGridTemplateColumn.Header>
      <Label Content="{x:Static res:StringResources.language}" ToolTip="{x:Static tt:TooltipStrings.language}" HorizontalContentAlignment="Stretch"/>
   </DataGridTemplateColumn.Header>
   <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
         <Image Source="{Binding Language, Converter={StaticResource StringToPicture}}" Stretch="Uniform"/>
      </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>