【发布时间】:2016-10-21 18:39:52
【问题描述】:
我同时使用WinForm 和WPF。我试图弄清楚如何在单击按钮时在我的WinForm 应用程序中更新我的WPF textbox control 中的文本。使用WinForm 文本框我可以简单地做MyTextBox.Text = counter,但使用WPF 我不能。为什么以及如何做到这一点?
例如,如果我单击按钮,我的 WPF 文本框应该计数 1,2,3,4,5...但它没有。
WinForm 代码
int counter = 0;
private void counter_btn_Click(object sender, EventArgs e)
{
counter++;
CountTxtBx.Text = counter.ToString();
CountTxtBx.Update();
Console.WriteLine(counter);
}
WPF 用户文本框用户控件
<UserControl x:Class="textbox_Update.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:textbox_Update"
mc:Ignorable="d"
d:DesignHeight="50
" d:DesignWidth="239">
<Grid>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="50" TextWrapping="Wrap" Text="{Binding ElementName=textBox1, Path=Text}" VerticalAlignment="Top" Width="239" FontSize="20" Padding="0,7,0,0"/>
</Grid>
WinForms 中我的 WPF 用户控件设置
【问题讨论】:
-
您在名为 textBox 的文本框上有一个奇怪的绑定。
Text="{Binding ElementName=textBox1, Path=Text}"? -
我刚刚删除了它。到它的默认值
Text="TextBox",但它仍然不起作用。我输入了Text="{Binding ElementName=textBox1, Path=Text}",因为我认为我错过了那个。我正在尝试解决它。