gpt4 book ai didi

c# - WPF 列表框项目位图图像更新

转载 作者:行者123 更新时间:2023-11-30 23:14:15 25 4
gpt4 key购买 nike

我创建了一个带有图像缩略图查看器的图像编辑软件。我有一个旋转图像的功能,用户选择一张照片并单击按钮。问题是我不想刷新所有 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...
}

最佳答案

Image 元素的 Source 属性直接绑定(bind)到 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

关于c# - WPF 列表框项目位图图像更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43213201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com