XAML和WPF - 将变量传递给XAML Windows

时间:2022-04-08 00:03:43

I'm pretty new to WPF and i'm trying to load a XAML window and pass a variable to this XAML in its constructor or so, because i need it to load some items from this passed variable.

我是WPF的新手,我正在尝试加载XAML窗口并在其构造函数中将变量传递给此XAML,因为我需要它从此传递的变量中加载一些项目。

Could anyone point me to the direction of how to go about this please? How does one start up a XAML window and give it a variable please?

有人能指点我的方向吗?如何启动XAML窗口并为其提供变量?

Thanks in advance.. Erika

在此先感谢.. Erika

4 个解决方案

#1


7  

Try to use MVVM (Model-View-ViewModel) pattern.

尝试使用MVVM(Model-View-ViewModel)模式。

You need Model:

你需要型号:

class Person
{
    public string Name { get; set; }
}

View is your window or UserControl.

View是您的窗口或UserControl。

ViewModel can be something like that:

ViewModel可以是这样的:

class PersonViewModel : INotifyPropertyChanged
{
 private Person Model;
 public ViewModel(Person model)
 {
  this.Model = model;
 }

 public string Name
 {
  get { return Model.Name; }
  set
  {
   Model.Name = value;
   OnPropertyChanged("Name");
  }
 }

 public event PropertyChangedEventHandler PropertyChanged;
 private void OnPropertyChanged(string propertyName)
 {
  var e = new PropertyChangedEventArgs(propertyName);
  PropertyChangedEventHandler changed = PropertyChanged;
  if (changed != null) changed(this, e);
 }
}

Then you need to specify DataContext for your window:

然后,您需要为窗口指定DataContext:

View.DataContext = new PersonViewModel(somePerson);

And then define bindings in XAML:

然后在XAML中定义绑定:

<UserControl x:Class="SomeApp.View"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
<Grid>
    <TextBlock Text="{Binding Name}" />
<Grid>    
<UserControl>

MVVM makes code very elegant and easy.

MVVM使代码非常优雅和简单。

You can also try PRISM or Caliburn (http://caliburn.codeplex.com/) frameworks but they are more complex.

您也可以尝试使用PRISM或Caliburn(http://caliburn.codeplex.com/)框架,但它们更复杂。

#2


2  

Typically, in WPF, you'd create the items you want to load, and set the Window (or UserControl)'s DataContext to the class that contains your items. You can then bind directly to these in order to do custom display from the XAML.

通常,在WPF中,您将创建要加载的项目,并将Window(或UserControl)的DataContext设置为包含项目的类。然后,您可以直接绑定到这些,以便从XAML进行自定义显示。

#3


1  

My issue was that i wanted to access a class outside of the XAML window, not communicate with the relative code via Binding. For this, all i needed to do was create a static class to hold the value i required. Simple, yet the solution escaped me at that point. Its a rather dirty way to go round solving the problem but it does the trick.

我的问题是我想访问XAML窗口之外的类,而不是通过Binding与相关代码通信。为此,我需要做的就是创建一个静态类来保存我需要的值。很简单,但解决方案让我逃脱了。这是解决问题的一种相当肮脏的方式,但它可以解决问题。

I would like to thank both contributors who helped me understand the MVVM architecture as I hadnt really understood that previously.

我要感谢两位帮助我理解MVVM架构的贡献者,因为我之前并没有真正理解过。

Thanks so much for the prompt reply and sorry if my question was easily missunderstood! Sometimes im not very good at conveying my ideas..

非常感谢您的回复,如果我的问题很容易被误解,那就很抱歉!有时我不太善于传达我的想法..

#4


0  

If you want to see a bit more on MVVM then check out this (MVVM is really powerful and a great way to develop testable code):

如果你想在MVVM上看到更多,那么看看这个(MVVM非常强大,是开发可测试代码的好方法):

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

It should give you a very good idea of MVVM. There is an example you can download just under the heading where it says: Code download available from the MSDN Code Gallery.

它应该让你对MVVM有一个很好的了解。您可以在标题下面下载一个示例:代码下载可从MSDN代码库获得。

#1


7  

Try to use MVVM (Model-View-ViewModel) pattern.

尝试使用MVVM(Model-View-ViewModel)模式。

You need Model:

你需要型号:

class Person
{
    public string Name { get; set; }
}

View is your window or UserControl.

View是您的窗口或UserControl。

ViewModel can be something like that:

ViewModel可以是这样的:

class PersonViewModel : INotifyPropertyChanged
{
 private Person Model;
 public ViewModel(Person model)
 {
  this.Model = model;
 }

 public string Name
 {
  get { return Model.Name; }
  set
  {
   Model.Name = value;
   OnPropertyChanged("Name");
  }
 }

 public event PropertyChangedEventHandler PropertyChanged;
 private void OnPropertyChanged(string propertyName)
 {
  var e = new PropertyChangedEventArgs(propertyName);
  PropertyChangedEventHandler changed = PropertyChanged;
  if (changed != null) changed(this, e);
 }
}

Then you need to specify DataContext for your window:

然后,您需要为窗口指定DataContext:

View.DataContext = new PersonViewModel(somePerson);

And then define bindings in XAML:

然后在XAML中定义绑定:

<UserControl x:Class="SomeApp.View"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
<Grid>
    <TextBlock Text="{Binding Name}" />
<Grid>    
<UserControl>

MVVM makes code very elegant and easy.

MVVM使代码非常优雅和简单。

You can also try PRISM or Caliburn (http://caliburn.codeplex.com/) frameworks but they are more complex.

您也可以尝试使用PRISM或Caliburn(http://caliburn.codeplex.com/)框架,但它们更复杂。

#2


2  

Typically, in WPF, you'd create the items you want to load, and set the Window (or UserControl)'s DataContext to the class that contains your items. You can then bind directly to these in order to do custom display from the XAML.

通常,在WPF中,您将创建要加载的项目,并将Window(或UserControl)的DataContext设置为包含项目的类。然后,您可以直接绑定到这些,以便从XAML进行自定义显示。

#3


1  

My issue was that i wanted to access a class outside of the XAML window, not communicate with the relative code via Binding. For this, all i needed to do was create a static class to hold the value i required. Simple, yet the solution escaped me at that point. Its a rather dirty way to go round solving the problem but it does the trick.

我的问题是我想访问XAML窗口之外的类,而不是通过Binding与相关代码通信。为此,我需要做的就是创建一个静态类来保存我需要的值。很简单,但解决方案让我逃脱了。这是解决问题的一种相当肮脏的方式,但它可以解决问题。

I would like to thank both contributors who helped me understand the MVVM architecture as I hadnt really understood that previously.

我要感谢两位帮助我理解MVVM架构的贡献者,因为我之前并没有真正理解过。

Thanks so much for the prompt reply and sorry if my question was easily missunderstood! Sometimes im not very good at conveying my ideas..

非常感谢您的回复,如果我的问题很容易被误解,那就很抱歉!有时我不太善于传达我的想法..

#4


0  

If you want to see a bit more on MVVM then check out this (MVVM is really powerful and a great way to develop testable code):

如果你想在MVVM上看到更多,那么看看这个(MVVM非常强大,是开发可测试代码的好方法):

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

It should give you a very good idea of MVVM. There is an example you can download just under the heading where it says: Code download available from the MSDN Code Gallery.

它应该让你对MVVM有一个很好的了解。您可以在标题下面下载一个示例:代码下载可从MSDN代码库获得。