简体   繁体   中英

InvalidCastException on XAML FlipView Control

I have a FlipView control in my Store app and my FlipViewItem s are bound to an ObservableCollection.

Each FlipViewItem is a TextBox . At runtime, I get an InvalidCastException when I try this:

TextBox textbox = flipView.SelectedItem as TextBox;

Why is that? The FlipViewItem is a TextBox , and I am casting it to a TextBox . If I don't cast, I get an exception anyway. So what's the deal? How do I get a TextBox out of my FlipViewItem ?

Here is my XAML if it helps:

<FlipView x:Name="flipper"
          ItemsSource="{Binding secret}"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch"
          Background="{x:Null}"
          BorderBrush="{x:Null}"
          BorderThickness="0"

          SelectionChanged="flipper_SelectionChanged"
          Loaded="flipper_Loaded">
    <FlipView.ItemTemplate>
        <DataTemplate>
            <TextBox x:Name="flipViewItemTextBox"
                     Text="{Binding Content, Mode=TwoWay, 
                     UpdateSourceTrigger=PropertyChanged}"
                     IsSpellCheckEnabled="True"
                     IsDoubleTapEnabled="True"
                     IsColorFontEnabled="True"
                     IsRightTapEnabled="True"
                     IsTapEnabled="True"
                     IsTextPredictionEnabled="True"
                     Background="{x:Null}"
                     BorderBrush="{x:Null}"
                     BorderThickness="0"
                     TextWrapping="Wrap"
                     AcceptsReturn="True"
                     AllowDrop="True"
                     ScrollViewer.VerticalScrollBarVisibility="Auto"
                     ScrollViewer.VerticalScrollMode="Auto"

                     TextChanged="flipViewItemTextBox_TextChanged"
                     />
        </DataTemplate>
    </FlipView.ItemTemplate>
</FlipView>

Normaly, your SelectedItem is your item from ItemSource. In your case it's should be a secret item.

Didn't try it with flipview, but I talk about datagrid and other container like datagrid.

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