将文本框值绑定到wpf中的模型

时间:2015-12-11 09:55:47

标签: c# wpf

我有一个wpf应用程序。

我已成功将列表视图绑定到ObservableCollections。

我现在想用文本框做同样的事情,我只有一个标准的类模型(没有集合)。

所以,我有一个静态类:

namespace MyNameSpace
{
    public Static Class MyClass
    {
        public string MyField {get; set;}
    }
}  

并在我的标记中:

<TextBox Text="{Binding Path=MyNameSpace.MyClass.MyField}/>

但是我的标记中出现了这个声明的运行时错误?

附加: 作为回答,这是使用的代码(但它不起作用)

<Window x:Class="WpfApplication1.MainWindow"
        xmlns:vms="clr-namespace:WpfApplication1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <vms:Model></vms:Model>
    </Window.DataContext>
    <Grid>
        <StackPanel Orientation="Vertical">
        <TextBox Text="{Binding Path='MyField'}"/>
          <Button Content="Click Me!" Click="Button_Click" />  
        </StackPanel>
    </Grid>
</Window>



using System.ComponentModel;
namespace WpfApplication1
{
    public class Model : INotifyPropertyChanged
    {

        private string myField;
        public string MyField
        {
            get { return myField; }
            set
            {
                myField = value;
                Raise("MyField ");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void Raise(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        Model myModel = new Model();


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            myModel.MyField = "has worked";
        }
    }
}

2 个答案:

答案 0 :(得分:2)

假设您的类不是静态的,那么您可以通过以下步骤将TextBox绑定到控件:

  1. 定义命名空间:

    xmlns:vms="clr-namespace:Your namespace"
    
  2. 定义DataContext:

    <Window.DataContext><vms:MyClass></vms:MyClass></Window.DataContext>
    
  3. 绑定将如下:

    <TextBox Text="{Binding Path="MyField"}/>
    
  4. 如果必须将文本从控件更新为源,则VM应实现INotifyPropetyChanged。

    public class MyClass: INotifyPropertyChanged
    {   
    private string myField ;
    public string MyField 
    {
        get { return MyField ; }
        set 
        { 
            MyField = value;
            Raise("MyField ");
        }
    }
    
    public event PropertyChangedEventHandler PropertyChanged;
    
    public void Raise(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    }
    

答案 1 :(得分:1)

包含名称空间xmlns:ns="clr-namespace:MyNameSpace"

并使用<TextBlock Text="{Binding Source={x:Static ns:MyClass.MyField}}" />绑定绑定静态类。

整个测试代码如下: XML文件:

<Window x:Class="WpfApplication2.Test"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ns="clr-namespace:MyNameSpace"
        Title="Test" Height="300" Width="300">
    <DockPanel>
        <TextBlock Text="{Binding Source={x:Static ns:MyClass.MyField}}" Margin="10"/>
    </DockPanel>
</Window>

代码背后的代码

using System.Windows;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for Test.xaml
    /// </summary>
    public partial class Test : Window
    {
        public Test()
        {
            InitializeComponent();

        }
    }
}

namespace MyNameSpace
{
    public static class MyClass
    {
        static MyClass()
        {
            MyField = "Testing";
        }
        public static string MyField {get; set;}
    }
}