将图像控件绑定到可观察集合
本文关键字:观察 集合 绑定 图像 控件 | 更新日期: 2023-09-27 18:20:48
我正在尝试获取BitmapSource对象的ObservableCollection,以绑定到我的WPF表单上的一堆图像,但图像从未显示。。。我已验证图像正在加载到属性中,但我的绑定一定不正确。。。。。绑定路径应该如何编码?我曾经把每个图像绑定到一堆不同的对象上,但使用列表要好得多,所以我想用这种方式绑定它们。。。。。。
文本框正确显示ProgramPath属性,我只是无法获得绑定的图像源
XAML-在一个网格中,我有许多文本框和图像挨着
<TextBox HorizontalAlignment="Stretch" Margin="24,2,2,2" Name="TextBoxA" VerticalAlignment="Stretch"
Width="664" >
<Binding Path=".[0].ProgramPath" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:ExternalProgramValidator/>
</Binding.ValidationRules>
</Binding>
</TextBox>
<TextBox Grid.Row="1" HorizontalAlignment="Stretch" Margin="24,2,2,2" Name="TextBoxB" VerticalAlignment="Stretch" Width="664" >
<Binding Path=".[1].ProgramPath" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:ExternalProgramValidator/>
</Binding.ValidationRules>
</Binding>
</TextBox>
<Image Height="16 " HorizontalAlignment="Left"
Margin="4" Name="ImageA" Stretch="Fill" VerticalAlignment="Center" Width="16"
Source="{Binding Path=.[0].ProgramImage, UpdateSourceTrigger=PropertyChanged}">
</Image>
<Image Grid.Row="1" Height="16 " HorizontalAlignment="Left"
Margin="4" Name="ImageB" Stretch="Fill" VerticalAlignment="Center" Width="16"
Source="{Binding Path=.[0].ProgramImage, UpdateSourceTrigger=PropertyChanged}"/>
然后我有一个像这样的公开课
public class ExternalProgramsWindowData : INotifyPropertyChanged
{
private BitmapSource _ExtractPathImage(string fullPath)
{
BitmapSource returnedImage = null;
string pathForImage = string.Empty;
string[] s = fullPath.Split(new string[] { ".exe" }, StringSplitOptions.None);
if (s[0] != null)
{
pathForImage = s[0] + ".exe";
}
if (!string.IsNullOrWhiteSpace(pathForImage))
{
System.Drawing.Icon icon = IconExtractor.GetIcon(pathForImage, true);
returnedImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
icon.Handle,
System.Windows.Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
}
return returnedImage;
}
/// <summary>
/// A
/// </summary>
private string _programPath;
public string ProgramPath
{
get { return _programPath; }
set
{
_programPath = value;
Notify("ProgramPath");
ProgramImage = _ExtractPathImage(_programPath);
}
}
private BitmapSource _programImage;
public BitmapSource ProgramImage
{
get { return _programImage; }
set
{
_programImage = value;
Notify("ProgramImage");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected void Notify(string propName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
#endregion
}
在主窗口类中,我将网格绑定到这些类对象的集合
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class ExternalProgramsWindow : Window
{
public ObservableCollection<ExternalProgramsWindowData> WindowDataList { get; set; }
WindowDataList = new ObservableCollection<ExternalProgramsWindowData>();
ExternalPrograms_ExternalProgramsGrid.DataContext = WindowDataList;
然后我加载集合,ProgramPath属性被设置,它触发设置ProgramImage(它被正确地设置为图像,但窗口不显示图像)
foreach (ExternalProgram program in externalProgramList)
{
ExternalProgramsWindowData oExternalProgramsWindowData = new ExternalProgramsWindowData();
oExternalProgramsWindowData.ProgramPath = program.Path + " " + program.Arguments;
WindowDataList.Add(oExternalProgramsWindowData);
尝试通过从中继承来将自定义类用作ObservableCollection。在上面的代码中,我看不到oberservable集合和要绑定的属性之间的链接。
// assuming ExternalProgramsWindowData are your bitmap objects
public class SourceList : ObservableCollection<ExternalProgramsWindowData> {
}
public class ViewModel : INotifyPropertyChanged {
private SourceList mySourceList;
public SourceList MySourceList {
get { return mySourceList; }
set {
mySourceList = value;
FirePropertyChanged("MySourceList");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void FirePropertyChanged (string propertyName) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
然后在实际绑定中,您会遇到以下情况:
绑定的来源:对象ViewModel。绑定的路径:"MySourceList"。
我不熟悉ExternalProgramsWindowData数据,但可能您必须使用ValueConverter才能使绑定工作。
请注意,当视图模型数据与视图数据不兼容时,尽管绑定路径设置正确,但绑定将不起作用。
希望这有帮助:)