C#WPF - TransitioningContentControl与App.content

问题描述 投票:1回答:2

我使用MahApps(Metro Dark)的主题我看了这个主题的动画。

我走到了尽头:确实我创建了一个系统来在不同的UserControl之间切换,也就是说我只有一个窗口并点击不同的按钮,我有这个或者这样的UserControl。但是现在我使用这个系统开关,我没有动画(只有应用程序的开始)。

如何为UserControl(保持地铁主题)中的每个更改制作动画?

有人问我:使用TransitioningContentControl

但我让我的切换器像这样:

class Switcher
{
    public static UserControl WClient;
    public static UserControl WHome;
    public static UserControl WDataBase;

    public Switcher()
    {
        WClient = new Windows.Client();
        WHome = new Windows.Home();
        WDataBase = new Windows.DataBase();
    }

    public static void currentWindow(UserControl window, string color)
    {

        Window curApp = Application.Current.MainWindow;
        curApp.Content = window;

        if (window == WClient)
        {
            curApp.Title = "CLIENT - INFO-TOOLS - BY NAOGRAFIX";
        }
        else if (window == WDataBase)
        {
            curApp.Title = "DATABASE - INFO-TOOLS - BY NAOGRAFIX";
        }
        else
        {
            curApp.Title = "HOME - INFO-TOOLS - BY NAOGRAFIX";
        }

        currentColor(color); 
  }

}

现在,当我点击一个按钮(切换用户控件)时,我使用:

private void BtnDataBase_Click(object sender, RoutedEventArgs e)
{
     var color = "Red";

     if (DataBase.isConnected) { color = "Green"; }
     Switcher.currentWindow(Switcher.WDataBase, color);
}

我使用CONTENT,我不知道我是否可以使用TransitioningContentControl

救命 :)

n奥*

c# wpf animation user-controls mahapps.metro
2个回答
3
投票

您确实需要使用转换内容控件。您可以将其添加为窗口的直接内容,然后从主窗口按名称访问它,并改为更改其内容。

XAML

<metro:TransitioningContentControl x:Name="tContent"/>

C#

((ContentControl)curApp.FindName("tContent")).Content = window;

您将需要xml命名空间定义

xmlns:metro="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"

并且您可以使用TransitioningContentControl上的Transition属性更改过渡


0
投票

下面的WPF XAML显示了MahApps.Metro TransitioningContentControl的使用。

单击“内容”列表框以切换内容。

在“转换”列表框中选择转换效果,然后更改选定的“内容”以查看效果。

<Window x:Class="WpfMahApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="800">
    <Window.Resources>
        <TextBlock x:Key="Content1" Width="400" Height="200" Text="Content 1: TextBox" Background="Aqua" />
        <Canvas x:Key="Content2" Width="200" Height="400" Background="DarkOrange">
            <Ellipse Fill="YellowGreen" Stroke="Black"  Width="100" Height="200" />
            <Label Content="Content2: Canvas" />
        </Canvas>
        <Border x:Key="Content3" Width="100" Height="100" Background="Yellow" BorderBrush="Blue" BorderThickness="2" CornerRadius="4">
            <TextBlock Text="Content3: Border" />
        </Border>
    </Window.Resources>
    <StackPanel Orientation="Horizontal">
        <StackPanel Orientation="Vertical" >
            <CheckBox Margin="4" Content="Is Transitioning" IsChecked="{Binding ElementName=TransitioningContentControl,Path=IsTransitioning , Mode=OneWay}" />
            <StackPanel Orientation="Vertical" Margin="8">
                <TextBlock Text="Content" FontWeight="Bold"/>
                <ListBox Name="ContentSelection" HorizontalAlignment="Left">
                    <ListBoxItem Content="Content 1" Tag="{StaticResource Content1}" />
                    <ListBoxItem Content="Content 2" Tag="{StaticResource Content2}" />
                    <ListBoxItem Content="Content 3" Tag="{StaticResource Content3}" />
                </ListBox>
            </StackPanel>
            <StackPanel Orientation="Vertical" Margin="8">
                <TextBlock Text="Transition" FontWeight="Bold" />
                <ListBox Name="Transition" HorizontalAlignment="Left">
                    <ListBoxItem Content="Default" Tag="{x:Static mah:TransitionType.Default}"/>
                    <ListBoxItem Content="Normal" Tag="{x:Static mah:TransitionType.Normal}"/>
                    <ListBoxItem Content="Up" Tag="{x:Static mah:TransitionType.Up}"/>
                    <ListBoxItem Content="Down" Tag="{x:Static mah:TransitionType.Down}"/>
                    <ListBoxItem Content="Left" Tag="{x:Static mah:TransitionType.Left}" />
                    <ListBoxItem Content="Right" Tag="{x:Static mah:TransitionType.Right}"/>
                    <ListBoxItem Content="LeftReplace" Tag="{x:Static mah:TransitionType.LeftReplace}"/>
                    <ListBoxItem Content="RightReplace" Tag="{x:Static mah:TransitionType.RightReplace}"/>
                </ListBox>
            </StackPanel>
        </StackPanel>
        <mah:TransitioningContentControl Margin="8" 
            Name="TransitioningContentControl"
            Background="Beige" BorderBrush="Black" BorderThickness="1"
            Content="{Binding ElementName=ContentSelection, Path=SelectedValue.Tag}" 
            Transition="{Binding ElementName=Transition, Path=SelectedValue.Tag}" />
    </StackPanel>
</Window>

MAHApps Demo

© www.soinside.com 2019 - 2024. All rights reserved.