开发者

Data binding of TextBlock.Text in DataTemplate

开发者 https://www.devze.com 2023-02-25 07:19 出处:网络
I have a panorama control in which the panorama item is binded to a data template. The data template is:

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.3开发者_开发问答33" 
            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


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}"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号