一个WPF,binding的有关问题,编译通过了但是达不到小弟我的效果
一个WPF,binding的问题,编译通过了但是达不到我的效果。
---------------------------------------------
问题是,我希望在按下BUTTON2后,打出SUCCESS的字符,却没反应,BUTTON1也没反应。。。
------解决思路----------------------
。。。是不是无限递归了。。。
------解决思路----------------------

调试下看代码的执行
------解决思路----------------------
public string Result
{
get { return result; }
set { result = value;
if (PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
}
把Name改为Result即可
------解决思路----------------------
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name")); 应是这里的问题 了要与属性名称一样,改成Result
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
namespace WpfApplication2
{
class MyClass1 : INotifyPropertyChanged
{
public MyClass1()
{
result="null";
}
private string result;
public string Result
{
get { return result; }
set { result = value;
if (PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public class trans
{
[DllImport("trans.dll")]
public static extern int transmain(String exportpath, String importpath);
}
public partial class Main : Window
{
MyClass1 myout=new MyClass1 ();
private String exportpath;
private String importpath;
public Main()
{
InitializeComponent();
myout.Result = "there will show ,if transform is ok ";// Result 赋值应该放到Main的构造函数或者窗口的Loaded事件中赋值的。
//把结果binding上去。。。请注意,XAML里的binding并不能访问C#里的变量,而C#里的变量却可以访问XAML
Binding bind1 = new Binding();
bind1.Source = myout;
bind1.Path = new PropertyPath("Result");
this.MyLabel.SetBinding(Label.ContentProperty, bind1);
}
public class trans
{
[DllImport("trans.dll")]
public static extern int transmain(String exportpath, String importpath);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
//创建"打开文件"对话框
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
//设置文件类型过滤
dlg.Filter = "图片|*.jpg;*.png;*.gif;*.bmp;*.jpeg";
// 调用ShowDialog方法显示"打开文件"对话框
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
//获取所选文件名并在FileNameTextBox中显示完整路径
string filename = dlg.FileName;
//在image1中预览所选图片
BitmapImage image = new BitmapImage(new Uri(filename));
image1.Source = image;
//image1.Width = image.Width;
//image1.Height = image.Height;
//这段代码可以预览图片
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
int isornotsuccess = 1;
//int isornotsuccess = trans.transmain( exportpath, importpath);
if(isornotsuccess==1)
{
myout.Result = "success";
Binding bind2 = new Binding();
bind2.Source = myout;
bind2.Path = new PropertyPath("Result");
this.MyLabel.SetBinding(Label.ContentProperty, bind2); //这里写成功后的反应
}
}
private void textBox1_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) //key是一个类,这是一个类的判断
{
exportpath=this.textBox1.Text;
}
}
private void textBox2_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
}
}
}
}
---------------------------------------------
问题是,我希望在按下BUTTON2后,打出SUCCESS的字符,却没反应,BUTTON1也没反应。。。
------解决思路----------------------
。。。是不是无限递归了。。。
------解决思路----------------------
调试下看代码的执行
------解决思路----------------------
public string Result
{
get { return result; }
set { result = value;
if (PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
}
把Name改为Result即可
------解决思路----------------------
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name")); 应是这里的问题 了要与属性名称一样,改成Result