图像的条件DataTemplate

时间:2016-02-04 15:25:28

标签: wpf xaml

尝试使用相同的控件来显示带有图像的列而不使用,具体取决于布尔值(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>

1 个答案:

答案 0 :(得分:0)

您可以使用CellTemplateSelector根据对象选择所需的模板,查看this link以查看有关此主题的详细示例 祝你好运