DataTemplate 中 TextBlock.Text 的数据绑定
我有一个全景控件,其中全景项目绑定到数据模板。 数据模板是:
<DataTemplate x:Key="MyTemplate">
<Grid Height="546" Width="432">
<TextBlock
x:Name="FromValueTB"
TextWrapping="Wrap"
VerticalAlignment="Top"
FontSize="40"
TextAlignment="Right"
Padding="0,0,10,0"/>
<TextBlock
x:Name="FromValueUnitTB"
TextWrapping="Wrap"
Margin="0,50,15,0"
VerticalAlignment="Top"
FontSize="21.333"
TextAlignment="Right"
Foreground="{StaticResource PhoneAccentBrush}"/>
<TextBlock
x:Name="ToValueTB"
TextWrapping="Wrap"
Margin="0,74,0,0"
VerticalAlignment="Top"
TextAlignment="Right"
FontSize="40"
Padding="0,0,10,0"/>
<TextBlock
x:Name="ToValueUnitTB"
TextWrapping="Wrap"
Margin="0,119,15,0"
VerticalAlignment="Top"
TextAlignment="Right"
FontSize="21.333"
Foreground="{StaticResource PhoneAccentBrush}"/>
<TextBlock
x:Name="RestltTB"
TextWrapping="Wrap"
Margin="0,144,0,0"
VerticalAlignment="Top"
TextAlignment="Center"
FontSize="16"/>
</Grid>
</DataTemplate>
全景控件创建为:
<controls:Panorama
x:Name="mPanoramaControl"
Title="convertors"
Height="728"
Width="480"
Style="{StaticResource MyPanoramaStyle}">
<controls:PanoramaItem Header="item" ContentTemplate="{StaticResource MyTemplate}"/>
</controls:Panorama>
我面临的问题是我不知道如何将数据绑定到我的自定义类,这样如果我修改类中的属性,文本会改变。
我的自定义课程是这样的:
public class MyClass
{
public string line1{get;set;}
public string line2{get;set;}
public string line3{get;set;}
public string line4{get;set;}
public string line5{get;set;}
}
任何帮助将不胜感激。
谢谢
I have a panorama control in which the panorama item is binded to a data template.
The data template is:
<DataTemplate x:Key="MyTemplate">
<Grid Height="546" Width="432">
<TextBlock
x:Name="FromValueTB"
TextWrapping="Wrap"
VerticalAlignment="Top"
FontSize="40"
TextAlignment="Right"
Padding="0,0,10,0"/>
<TextBlock
x:Name="FromValueUnitTB"
TextWrapping="Wrap"
Margin="0,50,15,0"
VerticalAlignment="Top"
FontSize="21.333"
TextAlignment="Right"
Foreground="{StaticResource PhoneAccentBrush}"/>
<TextBlock
x:Name="ToValueTB"
TextWrapping="Wrap"
Margin="0,74,0,0"
VerticalAlignment="Top"
TextAlignment="Right"
FontSize="40"
Padding="0,0,10,0"/>
<TextBlock
x:Name="ToValueUnitTB"
TextWrapping="Wrap"
Margin="0,119,15,0"
VerticalAlignment="Top"
TextAlignment="Right"
FontSize="21.333"
Foreground="{StaticResource PhoneAccentBrush}"/>
<TextBlock
x:Name="RestltTB"
TextWrapping="Wrap"
Margin="0,144,0,0"
VerticalAlignment="Top"
TextAlignment="Center"
FontSize="16"/>
</Grid>
</DataTemplate>
The panorama control is created as:
<controls:Panorama
x:Name="mPanoramaControl"
Title="convertors"
Height="728"
Width="480"
Style="{StaticResource MyPanoramaStyle}">
<controls:PanoramaItem Header="item" ContentTemplate="{StaticResource MyTemplate}"/>
</controls:Panorama>
The problem I'm facing is that I'm not getting how to bind the data to my custom class such that if I modify the properties in my class, the text will change.
My custom class is something like this:
public class MyClass
{
public string line1{get;set;}
public string line2{get;set;}
public string line3{get;set;}
public string line4{get;set;}
public string line5{get;set;}
}
Any help will be appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了让您的用户界面在类的属性更改时更新,您需要实现 INotifyPropertyChanged。 MSDN 上有一篇很好的文章,指导您完成此操作。
假设使用此 DataTemplate 的控件的数据上下文设置为类的实例,那么您还需要将 Text 属性绑定到相关属性:
Text="{装订线1}"
In order for your user interface to update when the properties of your class change, you need to implement the
INotifyPropertyChanged
. There's a good article on MSDN that walks you through how to do this.Assuming that the data context for the control that is using this DataTemplate is set to an instance of your class, then you also need to bind the Text property to the relevant property:
Text="{Binding line1}"