更新二
问题已解决。谢谢。
对于一个简单的 Silverlight 打印预览引擎,我的 XAML 看起来像这样(摘录):
<Grid>
<TextBlock Text="{Binding IntroText}" />
<ItemsControl ItemsSource="{Binding DataItems}"
x:Name="DataItemsControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"
TextWrapping="Wrap"
Margin="0,2" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Text="{Binding OutroText}" />
</Grid>
我想确保所有内容都适合页面,因此我有一个简单的方法:
public bool FitsOnPrintPage(Size pageDimensions)
{
Measure(new Size(pageDimensions.Width, Double.PositiveInfinity));
return
DesiredSize.Height <= pageDimensions.Height &&
DesiredSize.Width <= pageDimensions.Width;
}
,我无法解释:
现在我们有一个奇怪的问题 绑定集合DataItems
是一个通用的object
列表。当包含简单字符串时,Measure(...)
方法按预期工作并返回正确计算的 DesiredSize
。到目前为止,一切正常。
但是,当有一个像这样的简单对象...
public class DataItem
{
public string Value1 { get; set; }
public string Value2 { get; set; }
}
...并将 TextBlock Binding 更改为 时,结果视图是相同的,但是 Measure(...)
方法不会返回预期值,Items 的高度始终为零。同样不起作用:保留文本绑定并覆盖 DataItem
的 ToString()
方法。查看有效,而测量无效。
然后,我尝试在 DataTemplate 或整个页面上使用 InvalidateMeasure()
或 UpdateLayout()
等方法强制重新计算,但没有成功。
你能解释一下吗?
更新
有趣的是:出于调试原因,我将一个简单的自定义 ValueConverter
附加到 TextBlock 的 Binding 上。当绑定 string
对象时,我可以看到 Measure(...)
正在触发绑定 - 它首先被解析(我可以看到调试器步入 ValueConverter)并随后进行测量。但是,当如上所述绑定自定义类时,Measure(...)
不会触及绑定,我将“稍后”进入 ValueConverter
的断点。 (必须找出具体时间)
这对您有任何帮助吗?
UPDATE II
Problem was solved. Thank you.
For a simple Silverlight printing preview engine, my XAML looks like this (excerpt):
<Grid>
<TextBlock Text="{Binding IntroText}" />
<ItemsControl ItemsSource="{Binding DataItems}"
x:Name="DataItemsControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"
TextWrapping="Wrap"
Margin="0,2" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Text="{Binding OutroText}" />
</Grid>
I want to ensure that everything fits on a page, therefore i have a simple method:
public bool FitsOnPrintPage(Size pageDimensions)
{
Measure(new Size(pageDimensions.Width, Double.PositiveInfinity));
return
DesiredSize.Height <= pageDimensions.Height &&
DesiredSize.Width <= pageDimensions.Width;
}
Now we have a strange problem here which I can't explain:
The bound collection DataItems
is a generic object
List. When containing simple strings, the Measure(...)
method works as expected and returns a properly calculated DesiredSize
. So far, everything is working.
However, when having a simple object like this...
public class DataItem
{
public string Value1 { get; set; }
public string Value2 { get; set; }
}
...and changing the TextBlock Binding to <TextBlock Text="{Binding Path=Value1}"...
the resulting view is identical, however the Measure(...)
method doesn't return the expected values, the height of the Items is always zero. Also not working: keep Text Binding and override DataItem
s ToString()
method. View working, Measure doesn't.
I was then trying to force a recalculation using methods like InvalidateMeasure()
or UpdateLayout()
on the DataTemplate or the whole page, without success.
Can you explain this?
UPDATE
Interesting: I've attached a simple custom ValueConverter
to the TextBlock's Binding just for debugging reasons. When a string
object is bound, I can see that Measure(...)
is triggering the Binding - it's resolved first (i can see the debugger stepping into the ValueConverter) and measured afterwards. But when binding a custom class as described above, Measure(...)
doesn't touch the Binding, i am stepping into the ValueConverter
s breakpoint "later". (Have to find out, when exactly)
Does this help you in any kind?
发布评论
评论(3)
答案很简单..您不是以“silverlight方式”工作
在Silverligth中 - 字符串是否适合屏幕宽度并不重要,如果字符串不适合,只需设置< code>TextBlock.Wrap 到
Wrap
...由于“旧的思维方式”,您对此有问题...
但如果您非常想要它,请尝试以下操作:
The answer is simple.. you working not in the 'silverlight way'
In Silverligth - it dosen't mather if string fits to the screen width or not, if string dosen't fit, just set
TextBlock.Wrap
toWrap
...You have problem with this becose of 'old way of thinking'...
But if you want it so much try this:
这是来自 http://silverpdf.codeplex.com/ 的代码
也许它会对您有所帮助,但您有修改它,使其可用。
Here is the code from http://silverpdf.codeplex.com/
Maybee it would help you, but you have to modify it, to make it usable.
解决了
问题是首先创建和计算页面控件,并在生成后添加到显示控件中,因为我想避免频繁的UI更新。 Ai_boy 甚至也提出了类似的建议< /a>,他试图通过使用独立的网格控件来解决问题 - 不幸的是,这被证明是一种误导性的方法。只有将生成的页面控件添加到可视化树后,它才会自动解析绑定,从而实现正确的尺寸测量。
希望这对任何人都有帮助。
Solved
the problem was that the page controls were created and calculated first, and added to the displaying control after generation, because i wanted to avoid frequent UI updates. Something similar was even suggested by Ai_boy, who was trying to solve the problem by using an independent
Grid
Control - unfortunately this turned out as a misleading approach. Only after the generated page control was added to the visual tree, it automatically resolves the Bindings resulting in a proper size measuring.Hope this helps anyone.