C#ItemsControl图像更新WPF绑定

时间:2016-10-30 15:33:21

标签: c# wpf binding itemscontrol

今天我尝试将ObservableCollection绑定到ItemsControl

所以我想用这个XAML用ItemsControl显示不同的图像:

<Window x:Class="CM_Gestion_photo_2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:CM_Gestion_photo_2"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid x:Name="GrRoot">
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="432,10,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Margin="0,40,0,0">
        <ItemsControl Name="icList">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Image x:Name="wpfimg" Source="{Binding Img_In}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Grid>

我正在编写不同的方法来查找我的图像,这个方法有效,我可以完成一个ObservableCollection的图像但我无法更新我的源代码来显示我的图像我写了这段代码:

namespace CM_Gestion_photo_2
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<Img> items = new ObservableCollection<Img>();
        GestionRecherche gRech = new GestionRecherche();
        //Dispatcher disp = Dispatcher.CurrentDispatcher;
        Thread thr;
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            gRech.evt_envoi_objimage += ajoutphoto;
            icList.ItemsSource = items;
        }


        public void ajoutphoto(BitmapImage img_file)
        {
            if (!this.Dispatcher.CheckAccess())
            {
                icList.GetBindingExpression(ItemsControl.ItemsSourceProperty).UpdateSource();
            }
            else
            {
                items.Add(new Img() { Img_In = img_file });
                this.Dispatcher.Invoke(delegate () { ajoutphoto(img_file); });
            }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            thr = new Thread(delegate () { gRech.ScanDossiers(@"D:\donnees\Pictures"); });
            thr.SetApartmentState(ApartmentState.STA);
            thr.Start();
        }
    }

    public class Img
    {
        public BitmapImage Img_In { get; set; }
    }
}

我没有看到我的iamges在显示

你能帮帮我们吗?

THKS

0 个答案:

没有答案