WPF ListBox项目BitmapImage更新

时间:2017-04-04 16:49:52

标签: c# wpf xaml

我用图像缩略图查看器创建图像编辑软件。我有一个旋转图像功能,用户选择一张照片,然后单击按钮。问题是我不想刷新所有ListBox。所以这段代码:

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

我想要INotifyPropertyChanged只为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...
}

1 个答案:

答案 0 :(得分:2)

Source元素的Image属性直接绑定到source属性:

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

...或使用创建并返回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