【问题标题】:Data Binding doesn't work in xaml数据绑定在 xaml 中不起作用
【发布时间】:2016-09-09 08:49:39
【问题描述】:

我尝试使用绑定在文本内容中显示 Hi。 但是,单击按钮时,它不起作用。 有人可以帮我解决问题吗? 谢谢。

1.XAML 代码:

<Window x:Class="Wpftest.binding.Window0"                          
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window0" Height="300" Width="300">
   <Grid>
      <TextBox x:Name="textBox2" VerticalAlignment="Top" Width="168" 
               Text="{Binding Source= stu,  Path= Name, Mode=TwoWay}"/>
   </Grid>
</Window>

2.类:

namespace Wpftest.binding.Model
{
   public class student : INotifyPropertyChanged 
   {
      public event PropertyChangedEventHandler PropertyChanged;        
      private string name;

      public string Name
      {
         get { return name; }

         set { name = value;

              if(this.PropertyChanged != null)
              {
                 this.PropertyChanged.Invoke(this, new     
                 PropertyChangedEventArgs("Name"));
              }
         }
       }
    }
}

3.XAML.cs:

 namespace Wpftest.binding
    {
        public partial class Window0 : Window
        {
            student stu;
            public Window0()
            {
                InitializeComponent();
                stu = new student();
           }

            private void button_Click(object sender, RoutedEventArgs e)
            {
                stu.Name += "Hi!";
            }
        }
    }

【问题讨论】:

  • 你能告诉我stu是什么吗?
  • 将 DataContext 分配添加到您的 ViewModel

标签: wpf xaml data-binding


【解决方案1】:

有很多方法可以实现您所需要的; 正确 方法很大程度上取决于您要创建的应用程序样式。我将演示两种需要对您提供的示例进行最小更改的方法:

方法一

DataContext 设置为stu 并绑定到Name 属性。

XAML.cs

    private student stu;

    public Window0()
    {
        InitializeComponent();
        stu = new student();
        DataContext = stu;
    }

XAML 代码

<TextBox Text="{Binding Path=Name, Mode=TwoWay}"/>

方法二

通常您会将DataContext 设置为窗口以外的某个对象(例如,如果您遵循 MVVM 模式,则为 ViewModel),但有时您可能需要将控件绑定到窗口的某些属性。在这种情况下,DataContext 不能使用,但您仍然可以使用RelativeSource 绑定到 Window 的属性。见下文:

XAML.cs

    // note this must be a property, not a field
    public student stu { get; set; }

    public Window0()
    {
        InitializeComponent();
        stu = new student();
    }

XAML 代码

<TextBox Text="{Binding Path=stu.Name, Mode=TwoWay, 
        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>

提示:如果您在 WPF 数据绑定方面遇到问题,那么查看调试器输出窗口以查看绑定跟踪消息通常会有所帮助。并且可以通过将此命名空间添加到 Window 元素来进一步增强调试

xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"

然后设置 TraceLevel 例如

<TextBox Text="{Binding Source=stu, diag:PresentationTraceSources.TraceLevel=High}"/>

【讨论】:

  • 嗨,Rob:感谢您的帮助。但是,我还有一个问题。为什么 XAML.cs 中的代码仍然可以工作,而不是使用 DataContext? XAML.cs 中的代码如下: Binding bind = new Binding();绑定.源 = stu; bind.Path = new PropertyPath("Name"); this.textBox2_Copy.SetBinding(TextBox.TextProperty, bind);
  • 嗨@JETTSAI。在 XAML 中声明绑定与直接在 C# 中以编程方式创建绑定略有不同。 XAML 绑定使用 BindingExtension 类将“{Binding xxx}”声明转换为绑定,并且正是此类解释了您的Source=stu。实际上,它将stu 解释为字符串文字“stu”。
  • 嗨,Rob:谢谢你的解释。
【解决方案2】:

基本上您需要将 DataContext 属性设置为您的Window。 例如:

public MainWindow()
{
   DataContext=new YourViewModel();
}

WindowDataContextView(XAML) 和ViewModel(C# 代码) 之间的一种通信方式

另外,可以在xaml中添加DataContext

<Window.DataContext>
  <local:YourViewModel/>
</Window.DataContext>

此外,您应该使用ButtonCommand 属性,而不是处理Click 事件。 Example can be seen here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多