我试图在列表视图数据项中显示图像以及数据。我能够在列表视图中看到文本项,但看不到图像。我正在将图像与图像文件路径绑定。相同的图像路径正在工作在同一页的列表视图外部可以正常显示。但是在列表视图内部没有显示图像。但是在列表视图中可见图像控件周围的边框(黄色)。请帮忙找到解决方法
<< / p>
ListView Name="lvUsers" Grid.Column="1" Grid.Row="1" Margin="0,5,0,0" Height="Auto"
Background="{x:Null}" BorderBrush="{x:Null}"
ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"
VirtualizingStackPanel.IsVirtualizing="True" ScrollViewer.CanContentScroll="True"
SelectionMode="Single" IsSynchronizedWithCurrentItem="True"
IsEnabled="True">
<ListView.View >
<GridView >
<GridViewColumn Width="50" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Border Width="30" Height="30" Background="Yellow" >
<Image Width="10" Height="10" Stretch="Fill" Source="{Binding Path=DisplayedImage, Mode=Default}" HorizontalAlignment="Center" Margin="0,0,0,0" />
</Border>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="120" DisplayMemberBinding="{Binding EmpName}">
<GridViewColumn.Header>
<GridViewColumnHeader Tag="EmpName" Click="lvUsersColumnHeader_Click">Name</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Border Width="30" Height="30" CornerRadius="10,10,10,10 " >
<Image x:Name="Avatar" Source="{Binding Path=DisplayedImage, Mode=Default}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,0,0" />
</Border>
</Grid>
</UserControl>
我的代码如下: 命名空间和页面初始化代码:
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Configuration;
using System.Data;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
public ProjectTracker()
{
InitializeComponent();
List<DataTable> items = new List<DataTable>();
items.Add(loademp());
lvUsers.ItemsSource = loademp().DefaultView;
DataContext = new MainSideBar_viewModel();
}
视图模型中用于图像源绑定的对象:
public string DisplayedImage
{
get { return @"C:\faisal\Xerox2014\wpf\gsktid\P11376fs2.JPG"; }
}
答案 0 :(得分:0)
您可以使用RelativeSource
从CellTemplate
内绑定到视图模型的属性:
<GridViewColumn Width="50" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Border Width="30" Height="30" Background="Yellow" >
<Image Width="10" Height="10" Stretch="Fill"
Source="{Binding Path=DataContext.DisplayedImage, RelativeSource={RelativeSource AncestorType=ListView}}"
HorizontalAlignment="Center" Margin="0,0,0,0" />
</Border>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
DataContext
中的默认Image
是ItemsSource
中的一项,并且显然没有DisplayedImage
属性。