问题描述
限时送ChatGPT账号..我已经创建了数据透视页面,我希望在顶部的 2 个文本块中显示一些信息.我已经创建了 DataTemplate 并为文本字段设置了 x:names.但是我无法从页面构造函数寻址到此文本块并将文本放在那里.为什么?我怎么能欺骗这种情况?我需要在页面加载时将信息放入文本块.
I've created pivot page and I wish to display in the 2 text blocks at the top some information. I've created DataTemplate and set x:names for text fields. But I can't adress from page constructor to this text blocks and put there text. Why? And how can I cheat this situation? I need to put to text blocks information while page are loading.
<controls:Pivot.TitleTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="textBlock_eventID" TextWrapping="NoWrap" Margin="0"/>
<TextBlock x:Name="textBlock_eventName" TextWrapping="NoWrap" Opacity="0.7"/>
</StackPanel>
</DataTemplate>
</controls:Pivot.TitleTemplate>
推荐答案
使用 DataTemplate 的目的是定义业务对象的表示.也就是说,如果你有一个像下面这样的类:
The point of using a DataTemplate is to define the presentation of a business object. That is to say that if you have a class like the following:
public class MyClass
{
public string StringOne { get; set; }
public string StringTwo { get; set; }
}
如果您将 Pivot 控件的 Title 属性设置为此类的一个实例,则可以使用 DataTemplate 来定义您希望它的外观.
If you set the Title property of your Pivot control to an instance of this class, a DataTemplate can be used to define what you want it to look like.
在您的情况下,模板可能如下所示:
In your case, the template could look like this:
<controls:Pivot.TitleTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding StringOne}" TextWrapping="NoWrap" Margin="0"/>
<TextBlock Text="{Binding StringTwo}" TextWrapping="NoWrap" Opacity="0.7"/>
</StackPanel>
</DataTemplate>
</controls:Pivot.TitleTemplate>
然后从代码隐藏中,您将设置 Pivot 控件的 Title 属性.像这样:
Then from the code-behind, you would set the Title property of the Pivot control. Something like this:
myPivotControl.Title = new MyClass
{
StringOne = "Some String",
StringTwo = "Some other string"
};
<小时>
还有另一种方法可以做到这一点,它允许您从后面的代码中简单地设置文本框,但这不涉及 DataTemplate.
There is another way you could do this, which would allow you to simply set the text boxes from the code behind, but this does not involve a DataTemplate.
像下面这样设置 Pivot 控件的 Title 属性,将使您能够按名称访问 TextBox:
Setting the Title property of your Pivot control like the following, will enable you to access the TextBox's by name:
<controls:Pivot.Title>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="textBlock_eventID" TextWrapping="NoWrap" Margin="0"/>
<TextBlock x:Name="textBlock_eventName" TextWrapping="NoWrap" Opacity="0.7"/>
</StackPanel>
</controls:Pivot.Title>
阅读这篇文章,了解有关 DataTemplates 的更多信息
这篇关于将文本设置为自定义标题页枢轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论