尝试使用相同的控件来显示带有图像的列而不使用,具体取决于布尔值(HistoryOn)。下一个xaml代码可以工作,但始终显示图像。
<DataGridTemplateColumnx:Name="dgtc">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding
Converter={StaticResource myDataRowToListConverter}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border x:Name="imageBorder" BorderThickness="1" BorderBrush="Black"
MouseLeave="imageBorder_MouseLeave"
MouseEnter="imageBorder_MouseEnter"Height="16">
<Image x:Name="myImage" Source="{BindingMyImagePath}"
MouseUp="Image_MouseUp" HorizontalAlignment="Center">
</Image>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
下一个xaml代码仅显示路径,而不显示图像。有什么问题?
<DataGridTemplateColumn x:Name="dgtc">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding
Converter={StaticResource myDataRowToListConverter}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding MyImagePath}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding HistoryOn}" Value="true">
<Setter Property="ContentTemplate"
Value="{StaticResource imagesOff}" />
</DataTrigger>
<DataTrigger Binding="{Binding HistoryOn}" Value="false">
<Setter Property="ContentTemplate"
Value="{StaticResource imagesOn}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
在Windows.Resources中:
<DataTemplate x:Key="imagesOn">
<Border x:Name="imageBorder1" BorderThickness="1" BorderBrush="Black"
MouseLeave="imageBorder_MouseLeave"
MouseEnter="imageBorder_MouseEnter"Height="16">
<Image x:Name="myImage" Source="{BindingMyImagePath}"
MouseUp="Image_MouseUp"HorizontalAlignment="Center">
</Image>
</Border>
</DataTemplate>
<DataTemplatex:Key="imagesOff">
<Border x:Name="imageBorder2" BorderThickness="1" BorderBrush="Black"
MouseLeave="imageBorder_MouseLeave"
MouseEnter="imageBorder_MouseEnter"Height="16">
</Border>
</DataTemplate>