代码之家  ›  专栏  ›  技术社区  ›  Rami Raddaoui

在WPF中绑定图像

  •  0
  • Rami Raddaoui  · 技术社区  · 11 年前

    我想在一个统一的网格上显示多个图像(我从互联网上获取它们的URL)。当我执行代码时,图像不会显示。 这是视频窗口:

    <Window x:Name="videos" x:Class="Navigateur.Presentation.Videos"
            xmlns:my="clr-namespace:Navigateur.Presentation"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window2" Height="207" Width="463" WindowStyle="None" ResizeMode="NoResize" Topmost="True" WindowState="Maximized">
    
        <Grid>
            <ItemsControl Margin="72,30,76,30" ItemsSource="{Binding images}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="4" Rows="3"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Image Source="{Binding}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>
    </Window>
    

    下面是代码:

    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    
    namespace Navigateur.Presentation
    {
        /// <summary>
        /// Logique d'interaction pour Window2.xaml
        /// </summary>
        public partial class Videos : Window
        {
            ObservableCollection<Image> images;
            public Videos()
            {
                InitializeComponent();
    
            }
            public ObservableCollection<Image> Images()
            {
                images = new ObservableCollection<Image>();
                ServiceReferenceVideo.VideoServiceClient wcf = new ServiceReferenceVideo.VideoServiceClient();
                foreach (var item in wcf.GetAllVideos())
                {
                    string link_thumb = wcf.GetThumbImage((wcf.GetVideoId(item.urlVideo)));
                    var wSource = new BitmapImage(new Uri(link_thumb));
                    var wImage = new Image { Source = wSource };
                    images.Add(wImage);
                }
                return images;
            }
    
    
        }
    }
    
    1 回复  |  直到 11 年前
        1
  •  0
  •   Sphinxxx    11 年前

    这里有很多问题:

    • 您的列表( ItemsControl )通过 Binding ,但您从未设置列表的 DataContext -绑定系统将尝试从中获取数据的对象(在您的情况下 Videos 窗口本身)。
    • 绑定仅适用于 公共财产 ,而不是像您的 images 收集
    • Images() 函数加载缩略图时,需要通知绑定系统绑定的集合( 公共财产 )通过实施 INotifyPropertyChanged 并提高 PropertyChanged 事件,或将属性设置为 DependencyProperty .
    • 按照现在的写法 图像() 函数创建一个集合 Image s、 ItemsControl使用它们作为新的 形象 s.使用 形象 作为 形象 是过分的(如果它真的起作用的话)。一 形象 happliy将例如URI作为源,因此不生成 形象 在代码中,您的 图像 集合可以是 link_thumb 网址。

    我建议您阅读WPF中的数据绑定。这里有很多资源。

    对于这个特殊的情况,因为所有的事情都是在 视频 窗口中,您可以完全跳过绑定,只需将图像列表强制输入到 项目控制 如果你给它起个名字:

    <ItemsControl Margin="72,30,76,30" x:Name="_imageList" >
        ...
    

    后面的代码:

    public void Images()
    {
        var images = new ObservableCollection<string>();
        var wcf = new ServiceReferenceVideo.VideoServiceClient();
        foreach (var item in wcf.GetAllVideos())
        {
            string link_thumb = wcf.GetThumbImage((wcf.GetVideoId(item.urlVideo)));
            images.Add(link_thumb);
        }
    
        _imageList.ItemsSource = images;
    }