DataBinding 在 WPF 中动态创建 jpeg

发布于 2024-12-22 14:54:38 字数 626 浏览 2 评论 0原文

在我的 WPF 应用程序中,我动态创建 jpeg 文件。它们被保存在 bin/Debug 或 bin/Release 中。

我有一个数据绑定 WrapPanel ,其中包含一个使用 ValueConverter 的图像控件,如下所示:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    return new Uri(value.ToString(), UriKind.RelativeOrAbsolute);
}

value 的示例值为 Images\400\26.jpg 我假设默认情况下在 bin\Debug 或 bin\Release 中查找。

我的问题是我似乎无法将图像控件数据绑定到动态创建的 jpeg。但我可以将数据绑定到我标记为 Include 且其 BuildActionContent 的其他 jpeg。

如何将数据绑定到编译时不存在的动态创建的图像?

In my WPF application I am creating jpeg files dynamically. They are being saved in bin/Debug or bin/Release.

I have a databound WrapPanel with an image control that is using a ValueConverter like so:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    return new Uri(value.ToString(), UriKind.RelativeOrAbsolute);
}

An example value for value is Images\400\26.jpg which I assume is by default looking in bin\Debug or bin\Release.

My problem is that I can't seem to be able to databind image controls to the dynamically created jpegs. But I can databind to other jpegs that I've marked as Include and whose BuildAction is Content.

How do I databind to the dynamically created images that aren't present at compile time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转角预定愛 2024-12-29 14:54:38

您可以创建图像路径集合,并向其中添加每个新创建的图像的路径。现在您可以将您的包装面板绑定到该集合。

您可以在 ViewModel 类中实现此集合,然后使此 ViewModel 类成为您的 DataContext,并使用转换器将您的 wrapPanel 绑定到该集合。

编辑:这是一个示例文件:

MainWindowViewModel.cs:

using System;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace wpfJpegBindingSample
{
    class MainWindowViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged 
        public event PropertyChangedEventHandler PropertyChanged;

        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
        #endregion

        //constructor
        public MainWindowViewModel()
        {
            m_imagesList = new ObservableCollection<Uri>();
        }

        //collection of images' Uris
        private ObservableCollection<Uri> m_imagesList;

        //property for the collection (so you can bind to it)
        public ObservableCollection<Uri> ImagesList
        {
            get 
            {
                return m_imagesList;
            }
        }

        //an Add method that update the bindings
        public void Add(Uri uri)
        {
            ImagesList.Add(uri);
            OnPropertyChanged("ImagesList");
        }

    }
}

MainWindow.xaml.cs:

using System.Windows;

namespace wpfJpegBindingSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = m_viewModel = new MainWindowViewModel();
        }

        private MainWindowViewModel m_viewModel;
    }
}

MainWindow.xaml:

<Window x:Class="wpfJpegBindingSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView ItemsSource="{Binding ImagesList}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <Image Source="{Binding}"/>
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

    </Grid>
</Window>

如您所见,在视图模型中我们定义了图像Uris的集合。
在后面的主窗口代码中,我们将该视图模型的对象设置为窗口数据上下文,并在 xaml 中将 listView(或您选择的任何其他控件)的 Item 源设置为绑定属性 ImagesList,在我们的数据中上下文即视图模型。

最后,我们可以修改您的动态 jpeg 创建器函数:

        private void dynamicallyJpegCreator(string newImagePath)
        {

            // Creating Image code
            // .....
            // Create new image at newImagePath

            m_viewModel.Add(new System.Uri(newImagePath, System.UriKind.RelativeOrAbsolute));

        }

此时您将获得一个列表视图,显示动态创建的图像列表

You can create a collection of Images Paths, and add to it every newly created image's path. now you can bind your wrappanel to this collection.

You can implement this collection in ViewModel class, and then make this ViewModel class be your DataContext, and bind your wrapPanel to that collection using your converter.

Edit: Here is a sample files:

MainWindowViewModel.cs:

using System;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace wpfJpegBindingSample
{
    class MainWindowViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged 
        public event PropertyChangedEventHandler PropertyChanged;

        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
        #endregion

        //constructor
        public MainWindowViewModel()
        {
            m_imagesList = new ObservableCollection<Uri>();
        }

        //collection of images' Uris
        private ObservableCollection<Uri> m_imagesList;

        //property for the collection (so you can bind to it)
        public ObservableCollection<Uri> ImagesList
        {
            get 
            {
                return m_imagesList;
            }
        }

        //an Add method that update the bindings
        public void Add(Uri uri)
        {
            ImagesList.Add(uri);
            OnPropertyChanged("ImagesList");
        }

    }
}

MainWindow.xaml.cs:

using System.Windows;

namespace wpfJpegBindingSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = m_viewModel = new MainWindowViewModel();
        }

        private MainWindowViewModel m_viewModel;
    }
}

MainWindow.xaml:

<Window x:Class="wpfJpegBindingSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView ItemsSource="{Binding ImagesList}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <Image Source="{Binding}"/>
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

    </Grid>
</Window>

As you can see, in the view model we defined a collection of the Uris of images.
In the main window code behind we set an object of that view model as our window data context, and in the xaml we set the Item source of our listView (or any other control you choose) to binding of the property ImagesList, at our data context which is the view model.

Finally, we can modify your dynamically jpeg creator function:

        private void dynamicallyJpegCreator(string newImagePath)
        {

            // Creating Image code
            // .....
            // Create new image at newImagePath

            m_viewModel.Add(new System.Uri(newImagePath, System.UriKind.RelativeOrAbsolute));

        }

At this point you'll get a listview displays list of dynamically created images

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文