WPF Tabcontrol:选项卡项选择上的滑动效果

发布于 2025-01-07 22:33:31 字数 87 浏览 0 评论 0原文

我的选项卡控件中有两个选项卡项目,我想在从另一个选项卡项目中选择一个选项卡项目时提供滑动效果(动画)。 如果有人有任何想法来实现这个动画,请告诉我。 提前致谢。

I am having two tab items in my tab control and i want to give sliding effects (Animation) while selecting one tab item form another.
Let me know if anyone has some idea's to achieve this animation.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁人与我共长歌 2025-01-14 22:33:31

http://social.msdn.microsoft.com/Forums/en-AU/wpf/thread/ed8801d8-51c4-4671-8b8c-86544c6d434d
这很有帮助并且常青..我不久前使用过它

这似乎是更新版本
http://blogs.intuidev.com/post/2010/01/26 /TabControlStyling_PartTwo.aspx

等等不确定滑动效果是什么意思?!有什么例子可以让我理解

http://social.msdn.microsoft.com/Forums/en-AU/wpf/thread/ed8801d8-51c4-4671-8b8c-86544c6d434d
this is helpful and evergreen..i have used it a while ago

This seems to be updated version
http://blogs.intuidev.com/post/2010/01/26/TabControlStyling_PartTwo.aspx

and more over not sure what you meant by sliding effects?! any examples for me to understand

永不分离 2025-01-14 22:33:31

xaml代码:

<Window x:Class="TabControlAnimation.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" WindowState="Maximized">

    <Grid HorizontalAlignment="Center" Name="maingrid">
        <Grid.Resources>
            <DataTemplate x:Key="TabTemplate">
                <Grid Name="gd">
                    <ContentControl Content="{Binding}"></ContentControl>
                </Grid>
            </DataTemplate>
        </Grid.Resources>
        <StackPanel Orientation="Horizontal">
            <TabControl ContentTemplate="{StaticResource TabTemplate}" Name="_menuTabControl" TabStripPlacement="Top" Width="auto" Height="{Binding ElementName=maingrid, Path=Height}" SelectionChanged="_menuTabControl_SelectionChanged">
                <TabItem Header="MyTabItem1">
                    <Grid Background="Red">
                        <TextBlock Text="This is tab1"></TextBlock>
                    </Grid>                      
                </TabItem>
                <TabItem Header="MyTabItem2">
                    <Grid Background="Green">
                        <TextBlock Text="This is tab2"></TextBlock>
                    </Grid>                      
                </TabItem>
                <TabItem Header="MyTabItem3">
                    <Grid Background="Yellow">
                        <TextBlock Text="This is tab3"></TextBlock>
                    </Grid>                      
                </TabItem>
                <TabItem Header="MyTabItem4">
                    <Grid Background="Violet">
                        <TextBlock Text="This is tab4"></TextBlock>
                    </Grid>                       
                </TabItem>
            <TabItem Header="MyTabItem5">
                <Grid Background="Blue">
                    <TextBlock Text="This is tab5"></TextBlock>
                </Grid>
            </TabItem>
        </TabControl>
        </StackPanel>
    </Grid> 

背后代码:

 public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
    }

   public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T:DependencyObject
    {
       if(depObj!=null)
       {
           for(int i=0;i<VisualTreeHelper.GetChildrenCount(depObj);i++)
           {
               DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
               if (child != null && child is T)
                   yield return (T)child;

               foreach (T childOfChild in FindVisualChildren<T>(child))
                   yield return childOfChild;
           }
       }
    }


   int prev = -1, curr = -1;
    private void _menuTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        curr = (sender as TabControl).SelectedIndex;
        if(prev!=curr)
        {
            foreach(var rectangle in FindVisualChildren<Grid>(this))
            {
                if(rectangle.Name=="gd")
                {
                    DoubleAnimation translate_x=null;
                    if(prev>curr)
                    {
                        translate_x = new DoubleAnimation()
                        {
                            From = -300,
                            To = 0,
                            Duration = TimeSpan.FromSeconds(0.3),

                        };
                    }
                    else
                    {
                        translate_x = new DoubleAnimation()
                        {
                            From = 300,
                            To = 0,
                            Duration = TimeSpan.FromSeconds(0.3),
                        };
                    }
                    var translate_y = new DoubleAnimation()
                    {
                        From = 0,
                        To = 0,
                        Duration = TimeSpan.FromSeconds(1),
                    };
                    TranslateTransform translateTransform1 = new TranslateTransform();
                    translateTransform1.BeginAnimation(TranslateTransform.XProperty, translate_x);
                    translateTransform1.BeginAnimation(TranslateTransform.YProperty, translate_y);
                    rectangle.RenderTransform = translateTransform1;
                    prev = curr;
                }
            }
        }
    }
}

xaml code:

<Window x:Class="TabControlAnimation.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" WindowState="Maximized">

    <Grid HorizontalAlignment="Center" Name="maingrid">
        <Grid.Resources>
            <DataTemplate x:Key="TabTemplate">
                <Grid Name="gd">
                    <ContentControl Content="{Binding}"></ContentControl>
                </Grid>
            </DataTemplate>
        </Grid.Resources>
        <StackPanel Orientation="Horizontal">
            <TabControl ContentTemplate="{StaticResource TabTemplate}" Name="_menuTabControl" TabStripPlacement="Top" Width="auto" Height="{Binding ElementName=maingrid, Path=Height}" SelectionChanged="_menuTabControl_SelectionChanged">
                <TabItem Header="MyTabItem1">
                    <Grid Background="Red">
                        <TextBlock Text="This is tab1"></TextBlock>
                    </Grid>                      
                </TabItem>
                <TabItem Header="MyTabItem2">
                    <Grid Background="Green">
                        <TextBlock Text="This is tab2"></TextBlock>
                    </Grid>                      
                </TabItem>
                <TabItem Header="MyTabItem3">
                    <Grid Background="Yellow">
                        <TextBlock Text="This is tab3"></TextBlock>
                    </Grid>                      
                </TabItem>
                <TabItem Header="MyTabItem4">
                    <Grid Background="Violet">
                        <TextBlock Text="This is tab4"></TextBlock>
                    </Grid>                       
                </TabItem>
            <TabItem Header="MyTabItem5">
                <Grid Background="Blue">
                    <TextBlock Text="This is tab5"></TextBlock>
                </Grid>
            </TabItem>
        </TabControl>
        </StackPanel>
    </Grid> 

Code Behind:

 public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
    }

   public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T:DependencyObject
    {
       if(depObj!=null)
       {
           for(int i=0;i<VisualTreeHelper.GetChildrenCount(depObj);i++)
           {
               DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
               if (child != null && child is T)
                   yield return (T)child;

               foreach (T childOfChild in FindVisualChildren<T>(child))
                   yield return childOfChild;
           }
       }
    }


   int prev = -1, curr = -1;
    private void _menuTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        curr = (sender as TabControl).SelectedIndex;
        if(prev!=curr)
        {
            foreach(var rectangle in FindVisualChildren<Grid>(this))
            {
                if(rectangle.Name=="gd")
                {
                    DoubleAnimation translate_x=null;
                    if(prev>curr)
                    {
                        translate_x = new DoubleAnimation()
                        {
                            From = -300,
                            To = 0,
                            Duration = TimeSpan.FromSeconds(0.3),

                        };
                    }
                    else
                    {
                        translate_x = new DoubleAnimation()
                        {
                            From = 300,
                            To = 0,
                            Duration = TimeSpan.FromSeconds(0.3),
                        };
                    }
                    var translate_y = new DoubleAnimation()
                    {
                        From = 0,
                        To = 0,
                        Duration = TimeSpan.FromSeconds(1),
                    };
                    TranslateTransform translateTransform1 = new TranslateTransform();
                    translateTransform1.BeginAnimation(TranslateTransform.XProperty, translate_x);
                    translateTransform1.BeginAnimation(TranslateTransform.YProperty, translate_y);
                    rectangle.RenderTransform = translateTransform1;
                    prev = curr;
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文