WPF绑定OneWayToSource设置源属性到""当DataContext的改变 [英] WPF binding OneWayToSource sets source property to "" when the DataContext is changed

查看:282
本文介绍了WPF绑定OneWayToSource设置源属性到""当DataContext的改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OneWayToSource绑定未表现如我所料,当我设定的目标控制在DataContext。源的属性被设置,而不是默认目标控件的属性的值。

我创建了一个标准的WPF窗口一个非常简单的程序,说明我的问题:

XAML

 <&StackPanel的GT;
  <文本框X:NAME =TB
    文本={绑定路径=文本模式= OneWayToSource,UpdateSourceTrigger =的PropertyChanged}
    框TextChanged =TextBox_TextChanged/>  <按钮内容=设置的DataContext点击=的button1_Click/>
< / StackPanel的>

MainWindow.cs

 公共部分类主窗口:窗口
{
   私人视图模型_vm =新视图模型();   私人无效的button1_Click(对象发件人,RoutedEventArgs E)
   {
      Debug.Print('设置的DataContext按钮,点击);
      tb.DataContext = _vm;
   }   私人无效TextBox_TextChanged(对象发件人,TextChangedEventArgs E)
   {
      Debug.Print(文本框改为+ tb.Text);
   }
}

ViewModel.cs

 公共类视图模型
{
   私人字符串_text;
   公共字符串文本
   {
      {返回_text; }
      组
      {
         Debug.Print(
            ViewModel.Text(旧值=+(_text?<&空GT;)+
            新值=+(价值?<&空GT;)+));
         _TEXT =价值;
      }
   }
}

文本框 TB 开始时与空的DataContext,因此,结合预计不会做任何事情。所以,如果我输入在文本框中的东西,说:X,则ViewModel.Text属性保持为空。

如果我然后点击设置的DataContext 按钮我本来期望的 ViewModel.Text 属性设置为的X TextBox.Text 属性。相反,它被设置为。当然是工作,因为如果我然后在文本框中键入Y,在X后的约束力,但它设置了 ViewModel.Text 属性设置为XY。

下面是输出(最后两行是反直觉的,因为计算的顺序,但他们肯定都键入Y之后立即发生)的例子:


  

文本框更改为X结果
  '设置的DataContext按钮,点击结果
  ViewModel.Text(旧值=<零>中新的价值=)结果
  ViewModel.Text(旧值=,新值= XY)结果
  文本框更改为XY


为什么要设置的 ViewModel.Text 属性,而不是X时,DataContext设置为?

我是什么做错了吗?我缺少的东西吗?我误解了一些关于绑定?

修改:我本来期望的输出是:


  

文本框更改为X结果
  '设置的DataContext按钮,点击结果
  ViewModel.Text(旧值=<零>中新的价值=的 X )结果
  ViewModel.Text(旧值= X 后,新的价值= XY)结果
  文本框更改为XY



解决方案

其错误或不perhabs。微软声称,它的设计。你先键入x,然后你通过点击按钮杀的DataContext因此为什么在TextBox拥有X和你viewModel.Text财产被新初始化(它为空)。当DataContext的改变吸气仍将被调用。你到底有没有机会来解决这个问题。

您可以使用,无论双向放话。

I have a OneWayToSource binding that is not behaving as I expected when I set the DataContext of the target control. The property of the source is being set to default instead of the value of the target control's property.

I've created a very simple program in a standard WPF window that illustrates my problem:

XAML

<StackPanel>
  <TextBox x:Name="tb"
    Text="{Binding Path=Text,Mode=OneWayToSource,UpdateSourceTrigger=PropertyChanged}"
    TextChanged="TextBox_TextChanged"/>

  <Button Content="Set DataContext" Click="Button1_Click"/>
</StackPanel>

MainWindow.cs

public partial class MainWindow : Window
{
   private ViewModel _vm = new ViewModel();

   private void Button1_Click(object sender, RoutedEventArgs e)
   {
      Debug.Print("'Set DataContext' button clicked");
      tb.DataContext = _vm;
   }

   private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
   {
      Debug.Print("TextBox changed to " + tb.Text);
   }
}

ViewModel.cs

public class ViewModel
{
   private string _Text;
   public string Text
   {
      get { return _Text; }
      set
      {
         Debug.Print(
            "ViewModel.Text (old value=" + (_Text ?? "<null>") + 
            ", new value=" + (value ?? "<null>") + ")");
         _Text = value;
      }
   }
}

The TextBox tb starts out with a null DataContext and therefore the binding is not expected to do anything. So if I type something in the text box, say "X", the ViewModel.Text property remains null.

If I then click the Set DataContext button I would have expected the ViewModel.Text property to be set to the "X" of the TextBox.Text property. Instead it is set to "". Certainly the binding is working because if I then type "Y" in the text box, after the "X", it sets the ViewModel.Text property to "XY".

Here is an example of the output (the last two lines are counter-intuitive because of the order of evaluation, but they definitely both occur immediately after typing the "Y"):

TextBox changed to X
'Set DataContext' button clicked
ViewModel.Text (old value=<null>, new value=)
ViewModel.Text (old value=, new value=XY)
TextBox changed to XY

Why is the ViewModel.Text property being set to "" instead of "X" when the DataContext is set?

What am I doing wrong? Am I missing something? Have I misunderstood something about binding?

Edit: I would have expected the output to be:

TextBox changed to X
'Set DataContext' button clicked
ViewModel.Text (old value=<null>, new value=X)
ViewModel.Text (old value=X, new value=XY)
TextBox changed to XY

解决方案

Its a bug or perhabs not. Microsoft claims its by design. You first type x and then you kill DataContext by clicking on Button hence why the TextBox holds x and your viewModel.Text property gets newly initialized (its empty). When on datacontext changed getter will still be called. In the end you have no chance to fix this.

You can however use two way and let it be.

这篇关于WPF绑定OneWayToSource设置源属性到&QUOT;&QUOT;当DataContext的改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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