简体   繁体   English

使用WPF在C#中动态更改图像

[英]Changing an Image dynamically in C# using WPF

I am having issues of having an image change dynamically. 我遇到了动态更改图像的问题。

Some background info: I have a list box that contains elements that can be selected. 一些背景信息:我有一个列表框,其中包含可以选择的元素。 These items are food categories. 这些项目是食品类别。 When a user clicks on one of the foods, I would like an image at a different location of the page to change. 当用户点击其中一种食物时,我希望页面其他位置的图像发生变化。

My Xaml file contains: 我的Xaml文件包含:

<Image Name="bigImage" Stretch="Fill" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center"/>

So when the user clicks a certain food category that "bigImage" would change: 因此,当用户单击某个食物类别时,“ bigImage”将发生变化:

FoodCollection foods = (FoodCollection)this.FindResource("FoodCategory");
            Food f = foods[foodListBox.SelectedIndex];
            Title_TextBlock.Text = f.Name;
            bigImage = f.MainImage;

In my food class I have a variable called Image m_mainImage: 在我的食物课中,我有一个名为Image m_mainImage的变量:

    public class Food
    {
        ...

        Image m_mainImage = new Image();
        String m_mainImagePath = string.Empty;

        ...

        public string MainImagePath{
            get { return m_mainImagePath; }
            set
            {
                m_mainImagePath = value;
                m_mainImage.BeginInit();
                m_mainImage.Source = new BitmapImage(new Uri(m_mainImagePath, UriKind.RelativeOrAbsolute));
                m_mainImage.EndInit();
                RaisePropertyChanged("MainImage");
                RaisePropertyChanged("MainImagePath");
            }
        }

        public Image MainImage
        {
            get { return m_mainImage; }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

    }
}

I read somewhere that I had to "resolve" the image, but I was unclear on what that meant. 我在某处阅读到必须“解析”图像的信息,但不清楚其含义。 I thought this would do it: 我以为这样可以做到:

m_mainImage.BeginInit();
                    m_mainImage.Source = new BitmapImage(new Uri(m_mainImagePath, UriKind.RelativeOrAbsolute));
                    m_mainImage.EndInit();

Sorry I am still new to WPF and C#. 抱歉,我还是WPF和C#的新手。 Thanks in advance. 提前致谢。

Setup some TwoWay Binding: 设置一些双向绑定:

Try: 尝试:

 bigImage.SetBinding(Image.SourceProperty, new Binding(){
      Source = f,
      Path = new PropertyPath("MainImage"),
      Mode=BindingMode.TwoWay
   });

Or: 要么:

<Image Name="bigImage" 
       Stretch="Fill" 
       Grid.Row="0" 
       Grid.Column="0" 
       HorizontalAlignment="Center" 
       VerticalAlignment="Center" 
       Source="{Binding Path=MyBitmapImage, Mode=TwoWay}"/>

public BitmapImage MyBitmapImage
{
   get
   {
      return new BitmapImage(new Uri(m_mainImagePath, UriKind.RelativeOrAbsolute));
   }
}

Have you set the DataContext of the window? 您是否设置了窗口的DataContext

Without this PropertyChanged won't be initialised, so: 没有此PropertyChanged将不会初始化,因此:

if (PropertyChanged != null)
    PropertyChanged(this, new PropertyChangedEventArgs(name));

will never fire as PropertyChanged is always null . 永远不会触发,因为PropertyChanged始终为null

Not sure if this will help, but shouldn't the call to m_mainImage.EndInit() in the setter for MainImagePath occur after the call for RaisePropertyChanged("MainImage") ... 不知道这是否会帮助,但不应该调用m_mainImage.EndInit()的setter方法MainImagePath呼吁后发生RaisePropertyChanged("MainImage") ...

Andrew 安德鲁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM