将图像控件绑定到可观察的集合

时间:2012-01-19 01:31:59

标签: c# wpf xaml data-binding observablecollection

我正在尝试将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);

1 个答案:

答案 0 :(得分:0)

尝试通过从中继承将自定义类用作 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));
        }
    }

}

然后在实际的Binding中你有以下情况:

绑定源:对象ViewModel。 绑定路径:“MySourceList”。

我不熟悉ExternalProgramsWindowData数据,但您可能必须使用ValueConverter才能使绑定工作。

请注意,当您的viewmodels数据与您的Views数据不兼容时,虽然绑定路径设置正确,但绑定不起作用。

希望这会有所帮助:)