[英]Binding image controls to observable collection
我正在嘗試獲取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 。 在上面的代碼中,我看不到可觀察的集合與要綁定的屬性之間的鏈接。
// 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才能使綁定起作用。
因此請注意,當您的Viewmodels數據與Views數據不兼容時,盡管綁定路徑設置正確,但是綁定將不起作用。
希望這可以幫助 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.