ラベルをリサイズするユーザーコントロール

ちょっとしたテスト

<UserControl x:Class="WpfControlLibrary1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <UserControl.Resources>
            <Storyboard x:Key="Storyboard1">
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="label" Storyboard.TargetProperty="(TextElement.FontSize)">
                <SplineDoubleKeyFrame KeyTime="00:00:00" Value="12"/>
                <SplineDoubleKeyFrame KeyTime="00:00:01" Value="32"/>
            </DoubleAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="label" Storyboard.TargetProperty="(FrameworkElement.HorizontalAlignment)">
                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static HorizontalAlignment.Center}"/>
                <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static HorizontalAlignment.Center}"/>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="label" Storyboard.TargetProperty="(FrameworkElement.VerticalAlignment)">
                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static VerticalAlignment.Center}"/>
                <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static VerticalAlignment.Center}"/>
            </ObjectAnimationUsingKeyFrames>
            <ThicknessAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="label" Storyboard.TargetProperty="(FrameworkElement.Margin)">
                <SplineThicknessKeyFrame KeyTime="00:00:00" Value="0"/>
                <SplineThicknessKeyFrame KeyTime="00:00:01" Value="0"/>
            </ThicknessAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="Storyboard2">
            <ThicknessAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="label" Storyboard.TargetProperty="(FrameworkElement.Margin)">
                <SplineThicknessKeyFrame KeyTime="00:00:00" Value="0"/>
                <SplineThicknessKeyFrame KeyTime="00:00:01" Value="0"/>
            </ThicknessAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="label" Storyboard.TargetProperty="(FrameworkElement.VerticalAlignment)">
                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static VerticalAlignment.Stretch}"/>
                <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static VerticalAlignment.Stretch}"/>
            </ObjectAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="label" Storyboard.TargetProperty="(TextElement.FontSize)">
                <SplineDoubleKeyFrame KeyTime="00:00:00" Value="32"/>
                <SplineDoubleKeyFrame KeyTime="00:00:01" Value="12"/>
            </DoubleAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="label" Storyboard.TargetProperty="(FrameworkElement.HorizontalAlignment)">
                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static HorizontalAlignment.Center}"/>
                <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static HorizontalAlignment.Center}"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>

        </UserControl.Resources>
        <UserControl.Triggers>
            <EventTrigger RoutedEvent="Mouse.MouseEnter" SourceName="label">
                <BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
            </EventTrigger>
            <EventTrigger RoutedEvent="Mouse.MouseLeave" SourceName="label">
                <BeginStoryboard x:Name="Storyboard2_BeginStoryboard" Storyboard="{StaticResource Storyboard2}"/>
            </EventTrigger>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                <BeginStoryboard x:Name="Storyboard2_BeginStoryboard1" Storyboard="{StaticResource Storyboard2}"/>
            </EventTrigger>
        </UserControl.Triggers>

        <Label x:Name="label" HorizontalAlignment="Center" >Hello world</Label>
</UserControl>

———–

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfControlLibrary1
{
    /// <summary>
    /// UserControl1.xaml の相互作用ロジック
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public UserControl1(string label)
        {
            InitializeComponent();
            this.label.Content = label;
        }
    }
}

—————

それをスタックパネルに入れる

foreach (var v in list)
{
    WpfControlLibrary1.UserControl1 userCont1 = new WpfControlLibrary1.UserControl1(v.Text);
    stackPanel1.Children.Add(userCont1);
}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です