将代码隐藏变量绑定到主窗口中的文本框中的文本 [英] Binding code-behind variable to text in textbox in main window

查看:138
本文介绍了将代码隐藏变量绑定到主窗口中的文本框中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是主窗口中文本框的xaml代码:

我在文本框中输入的文本绑定到代码后面的变量。 / p>

 < TextBox x:Name =Rotate1Text ={Binding ElementName = this,Path = testvalue}/> 

在主窗口中的代码中:

  private int testvalue {get;设置;} 

我知道如果是另一种方式,我将不得不更新源触发器在任何改变,但不知道当变化到任何输入的文本时该怎么办。

解决方案

试用代码:

  public partial class MainWindow:Window 
{
public DependencyProperty TestValueProperty = DependencyProperty.Register(testvalue,typeof(int),typeof(MainWindow));
public int testvalue
{
get {return(int)GetValue(TestValueProperty); }
set
{
SetValue(TestValueProperty,value);
}
}
public MainWindow()
{
InitializeComponent();
testvalue = 6;
}
}

在XAML

 < Window x:Class =WpfApplication1.MainWindow
x:Name =thisForm
xmlns =http:// schemas。 microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml>
< TextBox Text ={Binding ElementName = thisForm,Path = testvalue}/>
< / Window>

UPD:哦!当然!在CS和XAML代码中删除标签


I'm having issues binding text entered in a textbox to a variable in code behind.

Here's the xaml code for the textbox located in the main window:

<TextBox x:Name="Rotate1" Text="{Binding ElementName=this, Path=testvalue}" />

and in the code behind in main window:

private int testvalue { get; set;}

I know if it's the other way around I would have to update the source trigger on any change, but not sure what to do when it's changing variable to whatever the entered text is.

解决方案

try in code:

public partial class MainWindow : Window
{
  public DependencyProperty TestValueProperty = DependencyProperty.Register("testvalue", typeof(int), typeof(MainWindow));
  public int testvalue
  {
    get { return (int)GetValue(TestValueProperty); }
    set
    {
      SetValue(TestValueProperty, value);
    }
  }
  public MainWindow()
  {
    InitializeComponent();
    testvalue = 6;
  }
}

in XAML

<Window x:Class="WpfApplication1.MainWindow"
    x:Name="thisForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <TextBox Text="{Binding ElementName=thisForm, Path=testvalue}" />
</Window>

UPD: oh! of course! Remove Tag in CS and XAML code

这篇关于将代码隐藏变量绑定到主窗口中的文本框中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆