我正在尝试在我的WPF Window
上包含一个县海豹的图像,并将源绑定到一个BitmapImage
属性,但是图像根本不显示。我还想单击印章并使用LeftMouseButtonDown
事件将第一个图像更改为其他七个图像,每次更改之间有两秒的延迟。
我在this StackOverflow post中尝试了这些技术。
MainWindow.xaml中的图像定义:
<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
事件的类定义:
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
:
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
代码:
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));
}
如何连接ImageSource
的Binding
https://stackoverflow.com/questions/54067072
复制相似问题