如何使用 C# 动态更改 Flipview 对象

How to change Flipview object dynamically using c#

我正在为 windows phone 开发应用程序。我正在使用 flipview 来显示细节。此翻转视图包含三个控件并使用滑动更改视图。我想在单击按钮时更改视图。

查看我的代码:

<FlipView x:Name="flip" Grid.Row="1" Grid.ColumnSpan="4" Grid.Column="0" Margin="0,0,0,0.333" Grid.RowSpan="2">
            <FlipViewItem>
                    <Button Foreground="White" Background="Black" x:Name="btnAdd" Content="Add" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Tapped="btnAdd_Tapped" ></Button>
            </FlipViewItem>
            <FlipViewItem>
                <Button x:Name="btny2" Content="mov" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" Tapped="btny2_Tapped" ></Button>
            </FlipViewItem>
            <FlipViewItem>
                <Button x:Name="btnUpdate" Content="Upd" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right" Tapped="btnupdate_Tapped" ></Button>
            </FlipViewItem>
            <FlipViewItem>
                <Button x:Name="btnRemove" Content="Rem" Grid.Row="1" Grid.Column="3" HorizontalAlignment="Center" Tapped="btnRemove_Tapped" ></Button>
            </FlipViewItem>
        </FlipView>




private void btnAdd_Tapped(object sender, TappedRoutedEventArgs e)
        {
            flip.SelectedIndex++;

        }

        private void btnupdate_Tapped(object sender, TappedRoutedEventArgs e)
        {
            flip.SelectedIndex++;
        }

        private void btny2_Tapped(object sender, TappedRoutedEventArgs e)
        {
            flip.SelectedIndex++;
        }

        private void btnRemove_Tapped(object sender, TappedRoutedEventArgs e)
        {
            flip.SelectedIndex++;
        }
but it is not working at all.

在按钮点击事件中 increase/decrease 选定的索引..

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    SelectedIndex++;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    SelectedIndex--;
}

尝试这样的事情

在.xaml

<FlipView x:Name="flip" Grid.Row="1" Grid.ColumnSpan="4" Grid.Column="0" Margin="0,0,0,0.333" Grid.RowSpan="2">

    <Button Foreground="White" Background="Black" x:Name="btnAdd" Content="Add" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Tapped="btnAdd_Tapped" ></Button>

    <Button x:Name="btny2" Content="mov" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" Tapped="btny2_Tapped" ></Button>

    <Button x:Name="btnUpdate" Content="Upd" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right" Tapped="btnupdate_Tapped" ></Button>

    <Button x:Name="btnRemove" Content="Rem" Grid.Row="1" Grid.Column="3" HorizontalAlignment="Center" Tapped="btnRemove_Tapped" ></Button>

</FlipView>

在 .cs 中

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    var newItemIndex = (flip.SelectedIndex + 1)
    flip.SelectedIndex = newItemIndex;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    var newItemIndex = (flip.SelectedIndex - 1)
    flip.SelectedIndex = newItemIndex;
}

如果您将按钮放在 flipview 之外,然后尝试更改相同的索引然后它的工作,我发现了一些解决方法 请查看以下代码,使用 Grid 或 Canvas 我们可以轻松地将按钮放在 Flipview

xaml

 <Grid x:Name="LayoutRoot">



    <FlipView x:Name="SlideHub"  ManipulationMode="Rotate" FlowDirection="LeftToRight"     UseTouchAnimationsForAllNavigation="True" Grid.Row="1"  >

        <FlipViewItem  >

        </FlipViewItem>

        <FlipViewItem  >

        </FlipViewItem>

        <FlipViewItem  >

        </FlipViewItem>

        <FlipViewItem >
        </FlipViewItem>

    </FlipView>


    <Image Tapped="slideBankingNext_tapped" Grid.Row="0" Width="40" Margin="0 10 10 20" HorizontalAlignment="Right" VerticalAlignment="Bottom" Source="/Images/welcome_next.png"  />

</Grid>

C#代码

 private void slideBankingNext_tapped(object sender, TappedRoutedEventArgs e)
    {
        var newItemIndex = (SlideHub.SelectedIndex + 1);
            SlideHub.SelectedIndex = newItemIndex;
    }