我想获得一个ObservableCollection的BitmapSource对象绑定到我的WPF窗体上的一堆图像,并且该图像永远不会显示...我验证了图像正在加载到属性中,但我的绑定必须是不正确的......绑定路径应该如何编码?我曾经让每个图像绑定到一堆不同的对象,但是列表使用起来更好,所以我想用这种方式绑定它们......将图像控件绑定到可观察的集合
文本框正确显示ProgramPath属性,I就不能得到上述图像源的约束
XAML - 内网格我有很多文本框和图像的这样
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
}
相互
<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}"/>
然后,我有一个公共类旁边
在我网格绑定到那些类的集合主窗口类对象
/// <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);
[装订错误](http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30/debugging-data-bindings-in-a-wpf - 或Silverlight的application.aspx)? –
用更多代码查看更新的问题...... – theDoke
您仍然没有提及绑定错误,并且格式化现在又很糟糕:( –