110,535
社区成员
发帖
与我相关
我的任务
分享
<Window x:Class="WpfTest.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Grid>
<Image Height="150" HorizontalAlignment="Left" Margin="12,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" Source="{Binding Path=MyString}" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="40,176,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
using System;
using System.ComponentModel;
using System.Windows;
namespace WpfTest
{
/// <summary>
/// Window2.xaml 的交互逻辑
/// </summary>
public partial class Window2 : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private String m_mystring = "/Images/Bluehills.jpg";
public String MyString
{
get { return m_mystring; }
set {
m_mystring = value;
NotifyPropertyChanged("MyString");
}
}
public Window2()
{
InitializeComponent();
this.DataContext = this;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
MyString = "/Images/Sunset.jpg";
}
}
}
[/quote]
哦哦 谢谢谢谢!
有空再帮我看看这个吧http://bbs.csdn.net/topics/390547382<Window x:Class="WpfTest.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Grid>
<Image Height="150" HorizontalAlignment="Left" Margin="12,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" Source="{Binding Path=MyString}" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="40,176,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
using System;
using System.ComponentModel;
using System.Windows;
namespace WpfTest
{
/// <summary>
/// Window2.xaml 的交互逻辑
/// </summary>
public partial class Window2 : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private String m_mystring = "/Images/Bluehills.jpg";
public String MyString
{
get { return m_mystring; }
set {
m_mystring = value;
NotifyPropertyChanged("MyString");
}
}
public Window2()
{
InitializeComponent();
this.DataContext = this;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
MyString = "/Images/Sunset.jpg";
}
}
}
<Window x:Class="WpfTest.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Grid>
<TextBox Width="100" VerticalAlignment="Center" Text="{Binding Path=MyString}"/>
</Grid>
</Window>
using System;
using System.ComponentModel;
using System.Windows;
namespace WpfTest
{
/// <summary>
/// Window2.xaml 的交互逻辑
/// </summary>
public partial class Window2 : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private String m_mystring = "0";
public String MyString
{
get { return m_mystring; }
set {
m_mystring = value;
NotifyPropertyChanged("MyString");
}
}
public Window2()
{
InitializeComponent();
MyString = "1";
this.DataContext = this;
}
}
}
[/quote]
如果换成图片,Text="{Binding Path=MyString}"怎么改呢?<Window x:Class="WpfTest.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Grid>
<TextBox Width="100" VerticalAlignment="Center" Text="{Binding Path=MyString}"/>
</Grid>
</Window>
using System;
using System.ComponentModel;
using System.Windows;
namespace WpfTest
{
/// <summary>
/// Window2.xaml 的交互逻辑
/// </summary>
public partial class Window2 : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private String m_mystring = "0";
public String MyString
{
get { return m_mystring; }
set {
m_mystring = value;
NotifyPropertyChanged("MyString");
}
}
public Window2()
{
InitializeComponent();
MyString = "1";
this.DataContext = this;
}
}
}