我想獲得一個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
您仍然沒有提及綁定錯誤,並且格式化現在又很糟糕:( –