我不能将数据绑定在WPF / XAML中的局部变量 [英] I can't Data Bind to a local variable in WPF/XAML
问题描述
我想一个文本框来显示一个变量的值,当我点击它(从1到100的迭代),我不知道我错了,这样做的:
当我运行该项目没有显示在文本框中。
什么是在文本框中显示变量的最佳方式是什么?
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;使用System.Windows
;使用System.Windows.Controls的
;
使用System.Windows.Data;使用System.Windows.Documents
;
使用System.Windows.Input;使用System.Windows.Media
;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;使用System.Windows.Shapes
;
命名空间dataBindingTest
{
///<总结> $ B $为MainWindow.xaml
///< b ///交互逻辑; /总结>
公共部分类主窗口:窗口
{
公共主窗口()
{
的InitializeComponent();
}
公共字符串{会将myText获得;组; }
公共无效Button_Click_1(对象发件人,RoutedEventArgs E)
{
INT I = 0;
为(i = 0; I< 100;我++)
{
=会将myText i.ToString();
}
}
}
}
XAML
<窗口x:类=dataBindingTest.MainWindow
NAME =windowElement
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=主窗口HEIGHT =350WIDTH =525>
<网格和GT;
<按钮内容=按钮的HorizontalAlignment =左HEIGHT =106保证金=71,95,0,0VerticalAlignment =评出的WIDTH =125点击=Button_Click_1/> ;
< TextBlock的X:名称=myTextBox的HorizontalAlignment =左HEIGHT =106保证金=270,95,0,0TextWrapping =包装VerticalAlignment =评出的WIDTH =187文本={会将myText绑定,的ElementName = windowElement}/>
< /网格和GT;
< /窗GT;
您当前的会将myText
属性没有办法通知WPF绑定系统时,它的价值已经改变,因此的TextBlock
不会被更新。
如果你把它依赖属性,而不是它自动实现更改通知,并更改属性将会反映在的TextBlock
。
所以,如果你替换公共字符串{会将myText获得;组; }
所有这些代码它应该工作的:
公共字符串会将myText
{
{返回(串)的GetValue(myTextProperty); }
集合{的SetValue(myTextProperty,值); }
}
//使用的DependencyProperty作为后备存储会将myText。这使得动画,造型,绑定等等...
公共静态只读的DependencyProperty myTextProperty =
DependencyProperty.Register(会将myText的typeof(串)的typeof(窗口1),新PropertyMetadata(NULL)) ;
I want a textbox to display the value of a variable when I click it (an iteration of 1 to 100), I do not know what I am doing Wrong:
When I run the project nothing is displayed in the text box.
What is the best way to display variables in a text box?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace dataBindingTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public string myText { get; set; }
public void Button_Click_1(object sender, RoutedEventArgs e)
{
int i = 0;
for (i = 0; i < 100; i++)
{
myText = i.ToString();
}
}
}
}
XAML:
<Window x:Class="dataBindingTest.MainWindow"
Name="windowElement"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Height="106" Margin="71,95,0,0" VerticalAlignment="Top" Width="125" Click="Button_Click_1"/>
<TextBlock x:Name="myTextBox" HorizontalAlignment="Left" Height="106" Margin="270,95,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="187" Text= "{Binding myText, ElementName=windowElement}" />
</Grid>
</Window>
Your current myText
property has no way of notifying the WPF binding system when its value has changed, so the TextBlock
wont be updated.
If you make it a dependency property instead it automatically implements change notification, and the changes to the property will be reflected in the TextBlock
.
So if you replace public string myText { get; set; }
with all of this code it should work:
public string myText
{
get { return (string)GetValue(myTextProperty); }
set { SetValue(myTextProperty, value); }
}
// Using a DependencyProperty as the backing store for myText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty myTextProperty =
DependencyProperty.Register("myText", typeof(string), typeof(Window1), new PropertyMetadata(null));
这篇关于我不能将数据绑定在WPF / XAML中的局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!