繁体   English   中英

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

[英]Binding textbox values to a model in wpf

我有一个wpf应用程序。

我已成功将listview绑定到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";
        }
    }
}

假设您的类不是静态的,则可以通过以下步骤将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)); } } } 

包括名称空间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;}
    }
}  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM