为什么不会WPF数据绑定显示文本时的ToString()有一个合作对象? [英] Why won't WPF databindings show text when ToString() has a collaborating object?

查看:106
本文介绍了为什么不会WPF数据绑定显示文本时的ToString()有一个合作对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个简单的形式中,余绑定到许多不同的对象 - 一些进去列表框;一些的TextBlocks。

In a simple form, I bind to a number of different objects -- some go in listboxes; some in textblocks.

一对夫妇的这些对象都合作对象,在其上的ToString()在做其工作时方法调用 - 某种通常格式化

A couple of these objects have collaborating objects upon which the ToString() method calls when doing its work -- typically a formatter of some kind.

当我通过code我看一步,当绑定正在建立,

When I step through the code I see that when the databinding is being set up,

  1. 的ToString()
  2. 的合作对象不是,并返回预期的结果
  3. 在调试器检查时,对象返回的预期结果的ToString()
  1. ToString() is called
  2. the collaborating object is not null and returns the expected result
  3. when inspected in the debugger, the objects return the expected result from ToString()

文本做的没有的显示形式。

BUT the text does not show up in the form.

我看到的唯一共同点是,这些使用协作对象,而这显示预期只是从属性和包含对象的方法工作的其他绑定。

The only common thread I see is that these use a collaborating object, whereas the other bindings that show up as expected simply work from properties and methods of the containing object.

如果这是令人困惑,这里是code中的要点:

If this is confusing, here is the gist in code:

public class ThisThingWorks
{
    private SomeObject some_object;

    public ThisThingWorks(SomeObject s) { some_object = s; }

    public override string ToString() { return some_object.name; }
}

public class ThisDoesntWork
{
    private Formatter formatter;
    private SomeObject some_object;

    public ThisDoesntWork(SomeObject o, Formatter f) 
    {
        formatter = f; 
        some_object = o;
    }

    public override string ToString()
    {
        return formatter.Format(some_object.name);
    }
}

我要再次重申 - 的的ToString()方法的的在所有其他方面 - 但是当我绑定到对象WPF和指望它显示的结果的ToString(),我什么也没有。

Again, let me reiterate -- the ToString() method works in every other context -- but when I bind to the object in WPF and expect it to display the result of ToString(), I get nothing.

这个问题似乎是我所看到的,在的TextBlock 绑定错误行为。如果我在文本属性绑定到的DataContext 声明为接口类型,<$ C $的属性C>的ToString()永远不会被调用。如果我改变了财产申报的接口的实现,它将按预期工作。其他控件,如标签结合的的内容时正常工作属性设置为的DataContext 属性声明为实施或接口。

The issue seems to be what I see as a buggy behaviour in the TextBlock binding. If I bind the Text property to a property of the DataContext that is declared as an interface type, ToString() is never called. If I change the property declaration to an implementation of the interface, it works as expected. Other controls, like Label work fine when binding the Content property to a DataContext property declared as either the implementation or the interface.

由于这是迄今为止从标题对这个问题的内容去掉,我创建了一个新的问题在这里:<一href="http://stackoverflow.com/questions/2917878/why-doesnt-textblock-databinding-call-tostring-on-a-property-whose-compile-tim">http://stackoverflow.com/questions/2917878/why-doesnt-textblock-databinding-call-tostring-on-a-property-whose-compile-tim

Because this is so far removed from the title and content of this question, I've created a new question here: http://stackoverflow.com/questions/2917878/why-doesnt-textblock-databinding-call-tostring-on-a-property-whose-compile-tim

改了称呼:<一href="http://stackoverflow.com/questions/2917878/wpf-binding-behaviour-different-when-bound-property-is-declared-as-interface-vs-c">http://stackoverflow.com/questions/2917878/wpf-binding-behaviour-different-when-bound-property-is-declared-as-interface-vs-c

推荐答案

试试这些简单的变化:

先用这个版本的方法测试程序:

First test your program with this version of the method:

public override string ToString()
{
    return "This method's really being called."
}

如果说实际显示内容在用户界面,现在试试这个版本:

If that actually displays something in the UI, now try this version:

public override string ToString()
{
    Console.WriteLine(
       string.Format("some_object.name = {0}, formatter.Format(some_object.name) = {1}",
          some_object.name,
          formatter.Format(some_object.name));
    return formatter.Format(some_object.name);
}

如果这不会导致你找出什么是真的错了,我会非常惊讶。

If this doesn't lead you to figure out what's really wrong, I'll be extremely surprised.

这篇关于为什么不会WPF数据绑定显示文本时的ToString()有一个合作对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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