XAML有时图标不会显示在列表视图单元格中

时间:2014-02-22 20:48:40

标签: c# .net wpf xaml binding

我正在尝试使用DataTemplate在列表视图单元格中显示文本+图像。

<ListView 
        HorizontalAlignment="Stretch"
        VerticalContentAlignment="Stretch"
        HorizontalContentAlignment="Center"
        Name="PlaybackListView" 
        VerticalAlignment="Top"
        ItemsSource="{Binding Playbacks}"
        IsSynchronizedWithCurrentItem="True">

     <ListView.Resources>
        <DataTemplate x:Key="StatusCellTemplate">
           <WrapPanel>
              <TextBlock Name="StatusTextBlock" Width="Auto" Text="{Binding Status}" />
              <Image Source="{Binding Converter={StaticResource PlaybackViewItemToStatusImageSource}}" Height="14" Width="14" />
           </WrapPanel>        
        </DataTemplate>
     </ListView.Resources>

     <ListView.View>
        <GridView>
           <! -- Columns -->
           <GridViewColumn 
              Header="Status" 
              Width="130"
              CellTemplate="{StaticResource StatusCellTemplate}" />
           <! -- Columns -->
        </GridView>
     </ListView.View>

问题在于有时会显示图标,但有时却不是。即使使用相同的源路径。

这是我在绑定中使用的ValueConverter的转换方法:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
     object result = value;

     PlaybackViewItem item = value as PlaybackViewItem;
     if (item != null)
     {
        switch (item.Status)
        {
           case PlaybackStatus.FilePreparationFinished:
           case PlaybackStatus.RadioPreparationFinished:
           case PlaybackStatus.Started:
              result = Path.Combine(IconsPath, "SignCheck.png");
              break;
           case PlaybackStatus.Finished:
              // HasPlaybackErrors is always false
              result = Path.Combine(IconsPath, !item.HasPlaybackErrors ? "SignCheck.png" : "WarningYellow.png");
              break;
           case PlaybackStatus.RadioPreparationFailed:
           case PlaybackStatus.FilePreparatinoFailed:
           case PlaybackStatus.CannotBeStarted:
           case PlaybackStatus.Failed:
           case PlaybackStatus.Stopped:
              result = Path.Combine(IconsPath, "SignExclamation.png");
              break;
           default:
              result = null;
              break;
        }
     }
     else
     {
        result = null;
     }

     return result;
  }

Here u see that the image is not showing

在上图中,您可以看到,在一种情况下,即使图像具有相同的状态,也不会显示图像。

调试输出中也没有绑定错误或任何内容。

1 个答案:

答案 0 :(得分:0)

您还没有告诉我们您的图片在哪里,但如果它们位于项目根文件夹中名为Images的文件夹中,那么您只需在Converter中引用它们就像这样:

result = "/AppName;component/Images/ImageName.png";