在XAML中初始化UserControl

时间:2014-11-17 09:44:59

标签: wpf xaml user-controls

我想在几个用户控件之间切换,即UserControl1,UserControl2,UserControl3,使用几个按钮,其中每个按钮与一个特定的UserControl相关联。我想在XAML中执行此操作,而不使用CodeBehind文件。

我已经构建了一个非常原始的UserControl,其中包含简单的Sliders,RadioButtons和CheckBoxes。我将其命名为UserControl1,删除了相应的CodeBehind文件,并删除了x:Class属性。现在,我想将UserControl1集成到我的MainUserControl中。当我使用Blend执行此操作时,我没有收到错误;但是,预览中有一个感叹号。当我尝试在Blend中重新构建代码时,我收到一条警告,说我应该再次重新构建代码。有谁看到我在这里做错了什么?

我的MainUserControl看起来像这样:

<UserControl
    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:local="clr-namespace:TestApp;assembly=TestApp"
    mc:Ignorable="d"
    x:Name="UserControl"
    xmlns:cntrl="clr-namespace:TestApp"
    d:DesignWidth="640" d:DesignHeight="480">
    <UserControl.Triggers>
        <EventTrigger RoutedEvent="ToggleButton.Checked" SourceName="Cntrl1">
            <BeginStoryboard Storyboard="{StaticResource ActivateCntrl1}"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="ToggleButton.Checked" SourceName="Cntrl2">
            <BeginStoryboard Storyboard="{StaticResource ActivateCntrl2}"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="ToggleButton.Checked" SourceName="Cntrl3">
            <BeginStoryboard Storyboard="{StaticResource ActivateCntrl3}"/>
        </EventTrigger>
    </UserControl.Triggers>
    <Viewbox>
        <Grid x:Name="LayoutRoot" Height="540" Width="1440">
            <Grid.Background>
                <LinearGradientBrush EndPoint="0.916,0.431" StartPoint="0.022,0.879">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="White" Offset="1"/>
                </LinearGradientBrush>
            </Grid.Background>
            <Ellipse HorizontalAlignment="Left" Height="80" Margin="46.5,223.5,0,0" Stroke="Black" VerticalAlignment="Top" Width="80" StrokeThickness="0" Opacity="0.7">
                <Ellipse.Fill>
                    <RadialGradientBrush>
                        <GradientStop Color="#FF3250DC" Offset="0"/>
                        <GradientStop Color="#FF14143C" Offset="0.905"/>
                        <GradientStop Color="#FF282864" Offset="0.672"/>
                    </RadialGradientBrush>
                </Ellipse.Fill>
            </Ellipse>
            <Button Content="Button" HorizontalAlignment="Left" Margin="46,13.5,0,0" VerticalAlignment="Top" Width="75"/>
            <Grid HorizontalAlignment="Left" Height="482.5" Margin="756,28.5,0,0" VerticalAlignment="Top" Width="640">
                                <StackPanel x:Name="Cntrl1Badge" Margin="0,0,0,0" Orientation="Vertical" RenderTransformOrigin="0.5,0.5">
                        <StackPanel.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform/>
                                <RotateTransform/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </StackPanel.RenderTransform>
                        <Border x:Name="Cntrl1Border" Width="640" Height="482.5" BorderThickness="2">
                            <Border.BorderBrush>
                                <SolidColorBrush Color="#7F666666" Opacity="1"/>
                            </Border.BorderBrush>
                            <Border.Background>
                                <RadialGradientBrush Opacity="1">
                                    <GradientStop Color="#CCBEBEBE" Offset="0"/>
                                    <GradientStop Color="#E53C3096" Offset="1"/>
                                </RadialGradientBrush>
                            </Border.Background>
                            <cntrl:UserControl1/>
                        </Border>
                    </StackPanel>
                    <StackPanel x:Name="Cntrl2Badge" Margin="0,0,0,0" Orientation="Vertical" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Right" Width="640">
                        <StackPanel.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform/>
                                <RotateTransform/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </StackPanel.RenderTransform>
                        <Border x:Name="Cntrl2Border" Width="640" Height="482.5" BorderThickness="2">
                            <Border.BorderBrush>
                                <SolidColorBrush Color="#7F666666" Opacity="1"/>
                            </Border.BorderBrush>
                            <Border.Background>
                                <RadialGradientBrush Opacity="1">
                                    <GradientStop Color="#CCBEBEBE" Offset="0"/>
                                    <GradientStop Color="#E53C3096" Offset="1"/>
                                </RadialGradientBrush>
                            </Border.Background>
                            <cntrl:UserControl2/>
                        </Border>
                    </StackPanel>
                    <StackPanel x:Name="Cntrl3Badge" Margin="0,0,0,0" Orientation="Vertical" RenderTransformOrigin="0.5,0.5">
                        <StackPanel.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform/>
                                <RotateTransform/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </StackPanel.RenderTransform>
                        <Border x:Name="Cntrl3Border" Width="640" Height="482.5" BorderThickness="2" Margin="5,0">
                            <Border.BorderBrush>
                                <SolidColorBrush Color="#7F666666" Opacity="1"/>
                            </Border.BorderBrush>
                            <Border.Background>
                                <RadialGradientBrush Opacity="1">
                                    <GradientStop Color="#CCBEBEBE" Offset="0"/>
                                    <GradientStop Color="#E53C3096" Offset="1"/>
                                </RadialGradientBrush>
                            </Border.Background>
                            <cntrl:UserControl3/>
                        </Border>
                    </StackPanel>
            </Grid>
            <StackPanel HorizontalAlignment="Left" Height="441.25" Margin="262.5,89.25,0,0" VerticalAlignment="Top" Width="397">
                <ToggleButton x:Name="Cntrl1" Style="{StaticResource MenuButtonStyle}" Content="ALEX" Height="81.96"/>
                <ToggleButton x:Name="Cntrl2" Style="{StaticResource MenuButtonStyle}" Content="SEVGI" Height="81.96"/>
                <ToggleButton x:Name="Cntrl3" Style="{StaticResource MenuButtonStyle}" Content="MICHL" Height="81.96"/>
            </StackPanel>
        </Grid>
    </Viewbox>
</UserControl>

UserControl1看起来像这样:

<UserControl
    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"
    mc:Ignorable="d"
    x:Name="UserControl"
    d:DesignWidth="640" d:DesignHeight="482.5">

    <Grid x:Name="LayoutRoot">
        <Slider HorizontalAlignment="Left" Margin="47.5,16,0,0" VerticalAlignment="Top" Width="534" Height="27.956"/>
        <Slider HorizontalAlignment="Left" Margin="47.5,78.266,0,0" VerticalAlignment="Top" Width="534" Height="27.957"/>
        <Slider HorizontalAlignment="Left" Margin="47.5,145.616,0,0" VerticalAlignment="Top" Width="534" Height="27.956"/>
        <Slider HorizontalAlignment="Left" Margin="47.5,211.694,0,0" VerticalAlignment="Top" Width="534" Height="27.957"/>
        <Slider HorizontalAlignment="Left" Margin="47.5,275.231,0,0" VerticalAlignment="Top" Width="534" Height="27.957"/>
        <RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="76.144,362.913,0,0" VerticalAlignment="Top" Height="20.281" Width="167.586"/>
        <CheckBox Content="CheckBox" HorizontalAlignment="Left" Margin="80.236,423.908,0,0" VerticalAlignment="Top" Height="20.281" Width="139.985"/>
        <RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="358.489,364.183,0,0" VerticalAlignment="Top" Height="20.281" Width="167.585"/>
        <CheckBox Content="CheckBox" HorizontalAlignment="Left" Margin="360.534,425.179,0,0" VerticalAlignment="Top" Height="20.281" Width="139.986"/>
    </Grid>
</UserControl>

注意,为了清楚起见,我从MainUserControl中删除了样式。因此,此XAML可能无法正常工作。

0 个答案:

没有答案