首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何连接ImageSource的绑定?

如何连接ImageSource的绑定?
EN

Stack Overflow用户
提问于 2019-01-07 08:01:39
回答 1查看 85关注 0票数 0

我正在尝试在我的WPF Window上包含一个县海豹的图像,并将源绑定到一个BitmapImage属性,但是图像根本不显示。我还想单击印章并使用LeftMouseButtonDown事件将第一个图像更改为其他七个图像,每次更改之间有两秒的延迟。

我在this StackOverflow post中尝试了这些技术。

MainWindow.xaml中的图像定义:

代码语言:javascript
复制
<Image x:Name="imag_CountySeal" Margin="0,60,0,80" Grid.Row="0" 
    Grid.Column="0"
    Source="{Binding ImageSource, UpdateSourceTrigger=PropertyChanged,Mode=OneWay}"
    MouseLeftButtonDown="Mouse_Down_Seal"
    Width="165" Height="165" Visibility="Visible" />

具有ImageSource属性和PropertyChanged事件的类定义:

代码语言:javascript
复制
public class CountySeals : INotifyPropertyChanged
{
    private BitmapImage _ImageSource;
    public BitmapImage ImageSource
    {
        get { return _ImageSource; }
        set
        {
            _ImageSource = value;
            OnPropertyChanged(new PropertyChangedEventArgs("ImageSource"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }
}

MainWindow.xaml.cs开头的逻辑,用于为第一个镜像设置ImageSource

代码语言:javascript
复制
public partial class MainWindow : Window
{
    CountySeals seals;

    public MainWindow()
    {
        InitializeComponent();
        seals = new CountySeals();
        seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_l transparent.png", UriKind.Absolute));
        SizeChanged += new SizeChangedEventHandler(Window_SizeChanged);
        StateChanged += new EventHandler(Window_StateChanged);
        LocationChanged += new EventHandler(Window_LocationChanged);

MainWindow.xaml.cs中的Mouse_Down_Seal代码:

代码语言:javascript
复制
private void Mouse_Down_Seal(object sender, MouseButtonEventArgs e)
{
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_2 transparent.png", UriKind.Absolute));
    Thread.Sleep(2000);
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_3 transparent.png", UriKind.Absolute));
    Thread.Sleep(2000);
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_4 transparent.png", UriKind.Absolute));
    Thread.Sleep(2000);
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_5 transparent.png", UriKind.Absolute));
    Thread.Sleep(2000);
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_6 transparent.png", UriKind.Absolute));
    Thread.Sleep(2000);
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_7 transparent.png", UriKind.Absolute));
    Thread.Sleep(2000);
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_8 transparent.png", UriKind.Absolute));
}

如何连接ImageSourceBinding

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54067072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档