当ToString()有协作对象时,为什么WPF数据绑定不会显示文本? [英] Why won't WPF databindings show text when ToString() has a collaborating object?
问题描述
以一种简单的形式,我绑定到一些不同的对象 - 一些在列表框中;一些在文本块中。
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.
当我浏览代码时,我看到当数据绑定正在设置时,
When I step through the code I see that when the databinding is being set up,
-
ToString()
被称为 - 协作对象不
null
并在调试器中检查时返回预期结果 - ,对象从
ToString返回预期结果)
ToString()
is called- the collaborating object is not
null
and returns the expected result - when inspected in the debugger, the objects return the expected result from
ToString()
但是文字不
我看到的唯一常见的线程是使用协作对象,而显示为预期的其他绑定只能从属性和方法工作的包含对象。
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.
如果这是令人困惑的,这里是代码中的要点:
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);
}
}
再次让我重申 - $ $ c> ToString()方法在每个其他上下文中工作 - 但是当我绑定到WPF中的对象并希望显示
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
绑定中看到的错误行为。如果我将 Text
属性绑定到声明为接口类型的 DataContext
的属性, ToString()
从未被调用。如果我将属性声明更改为接口的实现,它将按预期工作。将 Content
属性绑定到 DataContext $时,其他控件,例如
Label
c $ c>属性被声明为实现或接口。
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.
因为这是远离这个问题的标题和内容,我创建了一个新的问题在这里: WPF绑定行为不同于bound属性被声明为接口vs类类型?
Because this is so far removed from the title and content of this question, I've created a new question here: WPF binding behaviour different when bound property is declared as interface vs class type?
更改了标题: WPF绑定行为是不同的
推荐答案
尝试这些简单的更改:
首先使用此版本的方法:
First test your program with this version of the method:
public override string ToString()
{
return "This method's really being called."
}
如果在UI中实际显示内容,现在尝试此版本: p>
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.
这篇关于当ToString()有协作对象时,为什么WPF数据绑定不会显示文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!