使用Winform更新WPF文本框用户控件中的文本

taji01

我同时使用WinFormWPF我试图找出如何在按钮单击时WPF textbox control在我的WinForm应用程序中更新文本使用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 x:Name="textBox" HorizontalAlignment="Left" Height="50" TextWrapping="Wrap" Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=CounterValue}" VerticalAlignment="Top" Width="239" FontSize="20" Padding="0,7,0,0"/>

在usercontrol后面的代码中,您添加了一个依赖项属性:

    public int CounterValue
    {
      get { return (int)GetValue(CounterValueProperty); }
      set { SetValue(CounterValueProperty, value); }
    }

    // Using a DependencyProperty as the backing store for CounterValue.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CounterValueProperty =
        DependencyProperty.Register("CounterValue", typeof(int), typeof(UserControl1), new PropertyMetadata(0));

  }

在WinForm Button.Click事件中,您可以像这样进行更新:

private void button1_Click(object sender, EventArgs e)
{
  (CountTxBx.Child as UserControl1).CounterValue++;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用WPF用户控件文本框中设置的参数调用C#类中的方法

来自分类Dev

动态将文本框插入到用户控件中

来自分类Dev

遍历C#Winform控件时,无法在富文本框控件上使用Rtb属性

来自分类Dev

属性未在文本框中更新-WPF

来自分类Dev

如何在WPF文本框中推迟更新

来自分类Dev

尝试更新wpf中的文本框时,UI冻结

来自分类Dev

文本框控件中的验证问题

来自分类Dev

文本框控件中的验证问题

来自分类Dev

使用变量中的数据更新 Excel 用户表单中的多个文本框

来自分类Dev

用户控件中来自同一控件的参考文本框

来自分类Dev

用户窗体称为用户窗体文本框控件的行为

来自分类Dev

在将数据插入到WPF的文本框中之前,在选项卡控件中使用延迟

来自分类Dev

使用箭头浏览DataGrid WPF中的文本框

来自分类Dev

使用箭头浏览DataGrid WPF中的文本框

来自分类Dev

遍历面板控件中的文本框控件

来自分类Dev

在WPF的UserControl中将焦点设置在文本框控件上

来自分类Dev

WPF在网格子级中的文本框控件中循环

来自分类Dev

当我在文本框中单击时如何在wpf中启用弹出控件?

来自分类Dev

用户清除WPF中的文本字段时,文本框未将值设置为null

来自分类Dev

用户控件标签和文本框从右向左流动

来自分类Dev

在WPF中绑定文本框

来自分类Dev

ListView WPF中的文本框

来自分类Dev

在WPF中动态添加文本框

来自分类Dev

在DataGrid WPF中查找文本框

来自分类Dev

在 WPF 中更改文本框颜色

来自分类Dev

使用文本框过滤WPF Datagrid

来自分类Dev

使用javascript更新文本框中的值

来自分类Dev

使用按钮更新templateField中的文本框

来自分类Dev

如何删除在WPF网格中动态创建的文本框控件?

Related 相关文章

热门标签

归档