////app.xaml.cs///////
public partial class App : Application
{
public string number = "null"
}
////这样就定义了一个全局变量/////////
使用的时候按照((App)System.Windows.Application.Current).number 这个格式引用即可。
比如你d窗的 TEXTBOX X:NAME="TEXTBOX1"
关闭d窗的BUTTON CLICK 事件内就写
((App)System.Windows.Application.Current).number=TEXTBOX1.Text
这样 TEXTBOX1的内容就传递到 全局变量number中了,
如何显示到USERCONTROL中 方法很多 你可以根据自己的需求去写 根据事件激活 写TIMER
数据绑定 等,我数据绑定 不是很好 用了个TWOWAY绑定 没成功实时改变 USERCONTROL的内容。希望以上解答对你有帮助。
首先创先一个实体类public class MyClass : INotifyPropertyChanged
{
private static string _myString = "sdfdsfssdfsdfsdfdsf"
public string MyString
{
get { return _myString}
set
{
_myString = value
OnPropertyChanged("MyString")
}
}
public event PropertyChangedEventHandler PropertyChanged
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName))
}
}
//然后把这个实体类绑定到页面的 DataContext
<TextBox Text="{Binding MyString}"/>
追问:
我从后台绑定的,但是它说我没有实现INOtifyPropertyChanged接口
追答:
2中方式
第一种
第二种
追问:
使用第一种方法的时候它说URI指向的是未包含在程序集中的命名空间...
所以我在后台绑定的,具体代码如下:
但是程序爆出如下异常,请问该怎么办?把绑定的方法从oneway改为onetime
就可以运行,请问是什么原因:
追答:
不是这么写的吧
简单的写法
this.DataContext = new MyClass()
MytTextBox.SetBinding(TextBox.TextProperty, new Binding("MyString"){Mode = BindingMode.OneWay})
你的那种写法就不需要绑定DataContext了
直接
Binding bd = new Binding() { Source = new MyClass(), Path = new PropertyPath("MyString"),Mode = BindingMode.OneWay }
BindingOperations.SetBinding(MytTextBox, TextBox.TextProperty, bd)
追问:
你好,按照你的方式试过了,还是抛出XamlParseException错误,然后我将OneWay的调用方法改为onetime就不会出错,应该还是INotiyPropertyChanged接口没有正确实现,请问我该怎么办?
this.TopMost=true 就可以了。把窗体对象定义为全局变量,给他赋空值,判断他是否为空,为空就初始化,不为空就直接 show().当窗体关闭的时候,在让这个对象等于空。如果只想显示一个,在初始化时,判断其他窗体是否为空就可以了。为空就初始化,不为空就跳过。
private MusicWindow WinObj
private void btn_SetMusicePage_Click(object sender, RoutedEventArgs e)
{
if (WinObj == null)
{
WinObj = new MusicWindow()
WinObj.Closed += new EventHandler(WinObj_Closed)
WinObj.Show()
}
}
void WinObj_Closed(object sender, EventArgs e)
{
WinObj = null
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)