简体   繁体   中英

Windows 8 TextBlock in DataTemplate

<FlipView Name="flipView"
            AutomationProperties.AutomationId="ItemsFlipView"
            AutomationProperties.Name="Item Details"
            TabIndex="1"
            Grid.Row="1"
            ItemsSource="{Binding}" Style="{StaticResource FlipViewStyle1}">
            <TextBlock Name="answerT" Text="{Binding question}"/>
            <FlipView.ItemContainerStyle>
                <Style TargetType="FlipViewItem">
                    <Setter Property="Margin" Value="0,137,0,0"/>
                </Style>
            </FlipView.ItemContainerStyle>
            <FlipView.ItemTemplate>
                <DataTemplate>
                    <TextBlock x:Name="flipTxt" Text="{Binding question}"/>
                </DataTemplate>
            </FlipView.ItemTemplate>
</FlipView>

I have the above FlipView in XAML defined. I want to get the information that i have in the "flipTxt" TextBlock in a string in C#.

Tried with VisualTreeHelper but i can't seem to understand exactly how it works.

As well, tried to create another textblock (answerT) that would read the same info and get the text from that one. Didn't work either.

Thanks

LE: This is how i did the binding, i get the data from MobileService.

private IMobileServiceTable<myObj> obj_tb = App.MobileService.GetTable<myObj>();
private ObservableCollection<myObj> obj_it;


var res= await obj_tb.ToListAsync();
obj_it = new ObservableCollection<myObj(res);
flipView.ItemsSource = obj_it;

Providing your FlipView is binding to its collection correctly you can access the SelectedItem in code on different events eg SelectionChanged event

private void flipView_SelectionChanged(object sender, Windows.UI.Xaml.Controls.SelectionChangedEventArgs e)
    {
        if (flipView != null)
        {
            var item = flipView.SelectedItem as MyObj;
            string question = item.Question;
            string answer = item.Answer;
        }
    }

Possible MyObj

public class MyObj
{
    public string Question { get; set; }
    public string Answer { get; set; }
}

or as youre using MobileServices you would probably have this if using Json.NET

public class MyObj
{
    [JsonProperty(PropertyName = "Question")
    public string Question { get; set; }

    [JsonProperty(PropertyName = "Answer")]
    public string Answer { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM