Rafael Rocha
Rafael Rocha

Reputation: 183

WPF ListBox Item BitmapImage Update

I create a image edit software with images thumbnail viewer. I have a function that is Rotate image, user select a photo and click on button. The problem is i don´t want to refresh all the ListBox. So this code:

ImageListbox.Items.Refresh(); //Very slow if i have more than 100 imgs.

I Want a INotifyPropertyChanged just for BitmapImage.

XAML:

<ListBox Grid.Row="0" x:Name="ImageListbox" VirtualizingPanel.IsVirtualizing="true" 
             VirtualizingPanel.VirtualizationMode="Recycling" SelectionChanged="ImageListbox_SelectionChanged" MouseDoubleClick="ImageListbox_MouseDoubleClick"
            ItemsSource="{Binding Path=Model}"  
            Background="AliceBlue" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Margin="0,0,2,0" Grid.RowSpan="2">
    <ListBox.ContextMenu>
        <ContextMenu x:Name="MenuContext">
            <MenuItem Header="Abrir imagem (Prova)" Click="MenuItem_Click"/>
            <MenuItem Header="Abrir imagem (Original)" Click="MenuItemOriginal_Click"/>
        </ContextMenu>
    </ListBox.ContextMenu>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel x:Name="stack1" Height="330" Width="285" Margin="2,2,2,2" >
                <WrapPanel>
                    <TextBlock Text="{Binding Path=ImgName}" Margin="5,0,0,0" FontSize="16" ></TextBlock>
                    <CheckBox Height="20" x:Name="chkUniqueId" IsChecked="{Binding Path=CheckBox}"  Margin="5,0,0,0" Click="chkUniqueId_Click"/>
                </WrapPanel>
                <Image  Margin="5,5,5,5" Height="300" Width="280" VerticalAlignment="Top"  >
                    <Image.Source>
                        <BitmapImage x:Name="ImageSource"  DecodePixelWidth="300" CacheOption="None" UriSource="{Binding Path=ImgPath}" />
                    </Image.Source>
                </Image>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

C#

public class ReportagemItems : ObservableCollection<ReportagemItem>
{
    public ReportagemItems() { }
}

public class ReportagemItem : INotifyPropertyChanged
{
    //i have more 2 properties here, not necessary for this example.

    private string _ImgName;
    public string ImgName
    {
        get { return this._ImgName; }
        set
        {
            if (this._ImgName != value)
            {
                this._ImgName = value;
                this.NotifyPropertyChanged("ImgName");
            }
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

//in Btn Click event i have:

List<ReportagemItem> changeItem = ImageListbox.Items.OfType<ReportagemItem>().Where(x => x.CheckBox).ToList();

foreach (var item in Model.Where(x => x.ImgPath == changeItem.First().ImgPath))
{
    item.CheckBox = false; //WORKS
    item.ImgName = "New Name"; //WORKS
    item.ImgPath = @"C:\Programa Provas\Destino\Performance15\Uteis\Thumb\1.JPG"; //DOESNT WORK...
}

Upvotes: 2

Views: 837

Answers (1)

mm8
mm8

Reputation: 169240

Bind the Source property of the Image element, either directly to the source property:

<Image Margin="5,5,5,5" Height="300" Width="280" VerticalAlignment="Top" Source="{Binding Path=ImgName}"/>

...or by using a converter that creates and returns a BitmapImage: https://social.msdn.microsoft.com/Forums/vstudio/en-US/ea1fd63b-738c-40ca-850e-994eb21dccea/binding-to-image-source-via-valueconverter-and-datacontext-in-usercontrol?forum=wpf

Upvotes: 2

Related Questions