簡體   English   中英

將代碼隱藏變量綁定到主窗口中文本框中的文本

[英]Binding code-behind variable to text in textbox in main window

我在將文本框中輸入的文本綁定到后面代碼中的變量時遇到問題。

這是位於主窗口中的文本框的xaml代碼:

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

並在主窗口后面的代碼中:

private int testvalue { get; set;}

我知道這是否是另一種方法,我必須在發生任何更改時更新源觸發器,但是不確定將變量更改為輸入的文本時該怎么做。

嘗試輸入代碼:

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代碼中的標簽

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM