2017-04-04 90 views
2

我使用圖像縮略圖查看器創建圖像編輯軟件。我有一個功能是旋轉圖像,用戶選擇一張照片,然後單擊按鈕。問題是我不想刷新所有ListBox。所以這個代碼:WPF ListBox項目位圖圖像更新

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

我只想爲BitmapImage一個INotifyPropertyChanged

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... 
} 

回答

0

It Works!但現在我有每行一個圖像......每行3個圖像enter image description here