清空对话框时,WPF-UserControl绑定不起作用 [英] WPF-UserControl Binding does not Work when colsing a Dialog

查看:47
本文介绍了清空对话框时,WPF-UserControl绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我是WPF的新手,所以请为我的经验不足感到抱歉,我希望我的问题不是太愚蠢,但是我有一个问题:
我编写了一个简单的WPF-UserControl,其中包含一个标签,一个图标和一个文本框.当然,文本框"的文本应在两个方向上都可绑定.加载控件时,它工作正常,但是关闭控件时,它不能正常工作.我想我忘了编写一段和平的代码,该代码无法将输入的文本发送回属性文本并强制将Changed Event传递到外部,但是我不知道该怎么做.
谁能帮我吗?

这是我的代码隐藏文件:

Hi!

I''m new in WPF, so please sorry about my inexperience, I hope, my Question is not too stupid, but I have a problem:
I wrote a simple WPF-UserControl, which has a Lable, an Icon and a TextBox. And of course, the text of the Textbox should be bindable in both directions. It works fine when loading the control, but not, when closing it. I think I forgot to write a peace of code that sends the entered Text back to the Property Text and forces a Changed Event to outside, but I don''t know how.
Can anyone help me?

Here is my Code-Behind File:

public partial class TextCtrl : UserControl
{
...

public static readonly DependencyProperty TextProperty =
         DependencyProperty.Register("Text", typeof(string), typeof(TextCtrl),
         new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,  OnTextChanged));

       public string Text
       {
           get { return (string)GetValue(TextProperty); }
           set { SetValue(TextProperty, value); }
       }
       private static void OnTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
       {
           TextCtrl tx = (TextCtrl)sender;
           tx.txtText.Text = eventArgs.NewValue.ToString();
       }



在使用控件的XAML中,我设置了对属性文本的绑定:



In the XAML where I use the Control, I Set a Binding to the Property Text:

<src:TextCtrl Text="{Binding Path=.Strasse}"/>


Strasse是我的ViewModel中的一个属性.

非常感谢您对我的帮助,br Alex Hahn


Strasse is a Property from my ViewModel.

Thanks a lot for helping me, br Alex Hahn

推荐答案

对我来说就像您忘记将Mode=TwoWay放入XAML代码片段中一样.



Looks to me like you forgot to put Mode=TwoWay into your XAML snippet.




嗨!

感谢您的回答!
默认情况下,模式"为双向",但是当将其设置为显式时,它也不起作用!
当然不是,因为输入的文本在TextCtrl.txtText.Text中,所以绑定是在TextCtrl.Text上设置的!
加载控件时,在OnTesxtChange(..)中设置TextCtrl.txtText.Text.
我需要的是一种将Text从TextCtrl.txtText.Text转换为TextCtrl.Text的可能性.

当下,我使用OnTextChanged上的EventHandler来执行此操作,例如:

< pre lang ="cs">公共TextCtrl()
{
InitializeComponent();
txtText.TextChanged + =新的TextChangedEventHandler(txtText_TextChanged);
}
void txtText_TextChanged(对象发送者,TextChangedEventArgs e)
{
如果(发件人是TextBox)
{
如果(this.Text.CompareTo((发送为TextBox).Text)!= 0)
{
this.Text =(发送者为TextBox).Text;
}
}
}</pre>



但我认为,有一个更好的解决方案. Anny Idea?

br来自奥地利
亚历克斯
Hi!

Thanks for your Answer!
The Mode is TwoWay by default, but also when he is set explicit, it does not work!
Of course not, because the Text entered is in TextCtrl.txtText.Text, the binding is set on TextCtrl.Text!
When Loading the Control, TextCtrl.txtText.Text is set in in OnTesxtChange(..).
What i need is a possibility to get the Text from TextCtrl.txtText.Text into TextCtrl.Text.

At the Moment I do this with an EventHandler on OnTextChanged like:

<pre lang="cs">public TextCtrl()
{
InitializeComponent();
txtText.TextChanged += new TextChangedEventHandler(txtText_TextChanged);
}
void txtText_TextChanged(object sender, TextChangedEventArgs e)
{
if (sender is TextBox)
{
if (this.Text.CompareTo((sender as TextBox).Text) != 0)
{
this.Text = (sender as TextBox).Text;
}
}
}</pre>



but I think, there is a better solution. Anny Idea?

br from Austria
Alex


再见!

我找到了解决方案:我忘记设置TextBox的DataContext了; P!

在构造函数中,我添加了以下内容:
< pre lang ="cs">
公共TextCtrl()
{
InitializeComponent();
TxtText.DataContext = this;
}
</pre>


现在可以正常使用了!

祝WPF一切顺利,一切顺利.
亚历克斯
Hi again!

I found the solution: I forgot to set the DataContext of my TextBox ;P !

In the Constructor I added this:
<pre lang="cs">
public TextCtrl()
{
InitializeComponent();
TxtText.DataContext = this;
}
</pre>


Now it works fine!

Good luck with WPF to all and best regards
Alex


这篇关于清空对话框时,WPF-UserControl绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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