通过参数化重用WPF用户控件不起作用

时间:2019-01-23 01:28:16

标签: wpf mvvm visual-studio-2008 user-controls dependency-properties

在MVVM应用程序中,我具有以下用户控制。

在WPF主窗口的不同区域中,我正在创建此用户控件的不同实例,因此我想对该用户控件进行参数设置,以便重用它,并在每次创建新实例时在其标签中显示不同的消息。它的。为此,我在用户控件代码的后面创建了一个名为MessageText的依赖项属性,并以用户控件样式将其绑定到标签上。

用户控制

<UserControl x:Class="My.Apps.WPF.Demo.Controls.UCBusy"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">

    <UserControl.Resources>
        <Color x:Key="FilledColor" A="255" B="222" R="176" G="196"/>
        <Color x:Key="UnfilledColor" A="0" B="222" R="176" G="196"/>           

        <Style x:Key="BusyAnimationStyle" TargetType="Control">
            <Setter Property="Background" Value="white" />          

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Control">
                        <ControlTemplate.Resources>
                            <Storyboard x:Key="Animation0" BeginTime="00:00:00.0" RepeatBehavior="Forever">
                                <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse0" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                    <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                    <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>

                            <Storyboard x:Key="Animation1" BeginTime="00:00:00.2" RepeatBehavior="Forever">
                                <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse1" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                    <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                    <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>

                            <Storyboard x:Key="Animation2" BeginTime="00:00:00.4" RepeatBehavior="Forever">
                                <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse2" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                    <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                    <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>

                            <Storyboard x:Key="Animation3" BeginTime="00:00:00.6" RepeatBehavior="Forever">
                                <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse3" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                    <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                    <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>

                            <Storyboard x:Key="Animation4" BeginTime="00:00:00.8" RepeatBehavior="Forever">
                                <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse4" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                    <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                    <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>

                            <Storyboard x:Key="Animation5" BeginTime="00:00:01.0" RepeatBehavior="Forever">
                                <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse5" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                    <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                    <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>

                            <Storyboard x:Key="Animation6" BeginTime="00:00:01.2" RepeatBehavior="Forever">
                                <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse6" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                    <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                    <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>

                            <Storyboard x:Key="Animation7" BeginTime="00:00:01.4" RepeatBehavior="Forever">
                                <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse7" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                    <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                    <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </ControlTemplate.Resources>

                        <ControlTemplate.Triggers>                            
                            <Trigger Property="IsVisible" Value="True">                                                                

                                <Trigger.EnterActions>                                    
                                    <BeginStoryboard Storyboard="{StaticResource Animation0}" x:Name="Storyboard0" />
                                    <BeginStoryboard Storyboard="{StaticResource Animation1}" x:Name="Storyboard1"/>
                                    <BeginStoryboard Storyboard="{StaticResource Animation2}" x:Name="Storyboard2"/>
                                    <BeginStoryboard Storyboard="{StaticResource Animation3}" x:Name="Storyboard3"/>
                                    <BeginStoryboard Storyboard="{StaticResource Animation4}" x:Name="Storyboard4"/>
                                    <BeginStoryboard Storyboard="{StaticResource Animation5}" x:Name="Storyboard5"/>
                                    <BeginStoryboard Storyboard="{StaticResource Animation6}" x:Name="Storyboard6"/>
                                    <BeginStoryboard Storyboard="{StaticResource Animation7}" x:Name="Storyboard7"/>
                                </Trigger.EnterActions>

                                <Trigger.ExitActions>
                                    <StopStoryboard BeginStoryboardName="Storyboard0"/>
                                    <StopStoryboard BeginStoryboardName="Storyboard1"/>
                                    <StopStoryboard BeginStoryboardName="Storyboard2"/>
                                    <StopStoryboard BeginStoryboardName="Storyboard3"/>
                                    <StopStoryboard BeginStoryboardName="Storyboard4"/>
                                    <StopStoryboard BeginStoryboardName="Storyboard5"/>
                                    <StopStoryboard BeginStoryboardName="Storyboard6"/>
                                    <StopStoryboard BeginStoryboardName="Storyboard7"/>
                                </Trigger.ExitActions>
                            </Trigger>
                        </ControlTemplate.Triggers>

                        <Border BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}" 
                                Background="{TemplateBinding Background}">

                            <Grid>
                                <Canvas Height="60" Width="60">
                                    <Canvas.Resources>
                                        <Style TargetType="Ellipse">
                                            <Setter Property="Width" Value="15"/>
                                            <Setter Property="Height" Value="15" />
                                            <Setter Property="Fill" Value="#009B9B9B" />
                                        </Style>
                                    </Canvas.Resources>

                                    <Ellipse x:Name="ellipse0" Canvas.Left="1.75" Canvas.Top="21"/>
                                    <Ellipse x:Name="ellipse1" Canvas.Top="7" Canvas.Left="6.5"/>
                                    <Ellipse x:Name="ellipse2" Canvas.Left="20.5" Canvas.Top="0.75"/>
                                    <Ellipse x:Name="ellipse3" Canvas.Left="34.75" Canvas.Top="6.75"/>
                                    <Ellipse x:Name="ellipse4" Canvas.Left="40.5" Canvas.Top="20.75" />
                                    <Ellipse x:Name="ellipse5" Canvas.Left="34.75" Canvas.Top="34.5"/>
                                    <Ellipse x:Name="ellipse6" Canvas.Left="20.75" Canvas.Top="39.75"/>
                                    <Ellipse x:Name="ellipse7" Canvas.Top="34.25" Canvas.Left="7" />
                                    <Ellipse Width="39.5" Height="39.5" Canvas.Left="8.75" Canvas.Top="8" Visibility="Hidden"/> 

                                    <Label Content="{Binding Path=MessageText}" 
                                           FontSize="17"
                                           Canvas.Left="60.5" Canvas.Top="11.5"
                                           HorizontalContentAlignment="Center" 
                                           VerticalAlignment="Center"/>

                                </Canvas>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>   

    <Control Style="{StaticResource BusyAnimationStyle}" />

</UserControl>

背后的用户控制代码

    public partial class UCBusy : UserControl
    {
        public UCBusy()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty MessageTextProperty =
DependencyProperty.Register("MessageText", typeof(string), typeof(UCBusy));
        public string MessageText
        {
            get
            {
                return this.GetValue(MessageTextProperty) as string;
            }
            set
            {
                this.SetValue(MessageTextProperty, value);
            }
        }
    }

然后在视图中,将刚创建的用户控件依赖项属性与视图模型中的属性绑定在一起:

查看模型

<!-- Instance 1 -->
<controls:UCBusy MessageText="{Binding Path=WaitMessageText_1}" />

<!-- Instance 2 -->
<controls:UCBusy MessageText="{Binding Path=WaitMessageText_2}" />

...以及视图模型中...

查看模型

            private string _waitMessageText_1 = "Hi there! I am message 1";
            private string _waitMessageText_2 = "Hi there! I am message 2";

            /// <summary>
            ///   Gets or sets the wait message text 1.
            /// </summary>
            public string WaitMessageText_1
            {
                get
                {
                    return _waitMessageText_1;
                }

                set
                {
                    if (_waitMessageText_1 == value) return;
                    _waitMessageText_1 = value;

                    OnPropertyChanged("WaitMessageText_1");
                }
            }

            /// <summary>
            ///   Gets or sets the wait message text 2.
            /// </summary>
            public string WaitMessageText_2
            {
                get
                {
                    return _waitMessageText_2;
                }

                set
                {
                    if (_waitMessageText_2 == value) return;
                    _waitMessageText_2 = value;

                    OnPropertyChanged("WaitMessageText_2");
                }
            }

最后,在视图模型中,我需要在需要时设置这些属性,但是我注意到,我的视图并没有使用我输入的新消息进行更新,这意味着用户控件中的标签不会显示任何内容。我究竟做错了什么?我想我在任何地方都缺少东西...

注意:就我而言,Datacontext始终是相同的(我将其设置为指向视图模型),因此无需在每次创建用户控件的新实例时都进行更改。

1 个答案:

答案 0 :(得分:1)

我希望您先看看我提供的先前答案,该答案针对自定义控件,属性,样式等做了很多步骤。It too is here at S/O

几个直接的问题...您的“样式”声明基于“控件”,但是您的类基于“控件”,但是您的自定义类明确是“ UCBusy”控件,因此“控件”不知道什么是“ MessageText”用于样式绑定。

此外,您得到它的部分原因是因为您声明为其他类类型。

根据我以前的建议进行了充分的示例编辑。 这是可以查看所有部分的源代码。我创建了一个新项目“ StackHelp”,并具有以下内容。请注意,合并的字典是指“ component / MyClasses…”子文件夹,在该子文件夹中创建了我的自定义类和相应的.xaml资源字典。

APP.XAML

<Application 
    x:Class="StackHelp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MyMainWindow.xaml">

    <Application.Resources>
        <!-- Generic up front for the entire application for the theme / styles to be used -->
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary  Source="pack://application:,,,/StackHelp;component/MyClasses/UCBusy.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MyClasses \ UCBusy.cs(在项目的子文件夹中)

using System.Windows;
using System.Windows.Controls;

namespace StackHelp.MyClasses
{
    public class UCBusy : UserControl
    {
        public UCBusy()
        {
            // just to default so style will allow us to see where this will present
            // even just during design time sample.
            MessageText = "My Sample Text";
        }

        public static readonly DependencyProperty MessageTextProperty
            = DependencyProperty.Register("MessageText",
            typeof(string), typeof(UCBusy));
        public string MessageText
        {
            get { return GetValue(MessageTextProperty) as string; }
            set { SetValue(MessageTextProperty, value); }
        }
    }
}

MyClasses \ UCBusy.xaml(xaml特别是“资源字典”项) 定义了两种样式...一种基本的样式,就像一个独立的标签,只是为了了解如何完成绑定...第二种样式专门用于制作精致的样式。 “ xmlns:myC”表示“ myC”别名是指在每个命名空间的项目的“ MyClasses”子文件夹中找到的类。

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:myC="clr-namespace:StackHelp.MyClasses">

    <!-- resource dictionary is in-itself a list of resources -->

    <!-- Just a SIMPLE Style to test the proper bindings... as basic as possible to test context working -->
    <Style TargetType="{x:Type myC:UCBusy}" x:Key="BusyAnimationStyleBasic">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type myC:UCBusy}" >
                    <Label Content="{TemplateBinding MessageText}" 
                        FontSize="17"
                        HorizontalContentAlignment="Center" 
                        VerticalAlignment="Center"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


    <Color x:Key="FilledColor" A="255" B="222" R="176" G="196"/>
    <Color x:Key="UnfilledColor" A="0" B="222" R="176" G="196"/>


    <!-- Now, your elaborate style with all animations...-->
    <Style TargetType="{x:Type myC:UCBusy}" x:Key="BusyAnimationStyle">
        <Setter Property="Background" Value="white" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type myC:UCBusy}" >

                    <ControlTemplate.Resources>
                        <Storyboard x:Key="Animation0" BeginTime="00:00:00.0" RepeatBehavior="Forever">
                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse0" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>

                        <Storyboard x:Key="Animation1" BeginTime="00:00:00.2" RepeatBehavior="Forever">
                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse1" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>

                        <Storyboard x:Key="Animation2" BeginTime="00:00:00.4" RepeatBehavior="Forever">
                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse2" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>

                        <Storyboard x:Key="Animation3" BeginTime="00:00:00.6" RepeatBehavior="Forever">
                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse3" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>

                        <Storyboard x:Key="Animation4" BeginTime="00:00:00.8" RepeatBehavior="Forever">
                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse4" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>

                        <Storyboard x:Key="Animation5" BeginTime="00:00:01.0" RepeatBehavior="Forever">
                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse5" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>

                        <Storyboard x:Key="Animation6" BeginTime="00:00:01.2" RepeatBehavior="Forever">
                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse6" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>

                        <Storyboard x:Key="Animation7" BeginTime="00:00:01.4" RepeatBehavior="Forever">
                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="ellipse7" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
                                <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </ControlTemplate.Resources>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsVisible" Value="True">

                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource Animation0}" x:Name="Storyboard0" />
                                <BeginStoryboard Storyboard="{StaticResource Animation1}" x:Name="Storyboard1"/>
                                <BeginStoryboard Storyboard="{StaticResource Animation2}" x:Name="Storyboard2"/>
                                <BeginStoryboard Storyboard="{StaticResource Animation3}" x:Name="Storyboard3"/>
                                <BeginStoryboard Storyboard="{StaticResource Animation4}" x:Name="Storyboard4"/>
                                <BeginStoryboard Storyboard="{StaticResource Animation5}" x:Name="Storyboard5"/>
                                <BeginStoryboard Storyboard="{StaticResource Animation6}" x:Name="Storyboard6"/>
                                <BeginStoryboard Storyboard="{StaticResource Animation7}" x:Name="Storyboard7"/>
                            </Trigger.EnterActions>

                            <Trigger.ExitActions>
                                <StopStoryboard BeginStoryboardName="Storyboard0"/>
                                <StopStoryboard BeginStoryboardName="Storyboard1"/>
                                <StopStoryboard BeginStoryboardName="Storyboard2"/>
                                <StopStoryboard BeginStoryboardName="Storyboard3"/>
                                <StopStoryboard BeginStoryboardName="Storyboard4"/>
                                <StopStoryboard BeginStoryboardName="Storyboard5"/>
                                <StopStoryboard BeginStoryboardName="Storyboard6"/>
                                <StopStoryboard BeginStoryboardName="Storyboard7"/>
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>


                    <!--<StackPanel Orientation="Horizontal">
                        <TextBlock Text="{TemplateBinding MessageText}"/>
                    </StackPanel>-->

                    <Border BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Background="{TemplateBinding Background}">

                        <Grid>
                            <Canvas Height="60" Width="60">
                                <Canvas.Resources>
                                    <Style TargetType="Ellipse">
                                        <Setter Property="Width" Value="15"/>
                                        <Setter Property="Height" Value="15" />
                                        <Setter Property="Fill" Value="#009B9B9B" />
                                    </Style>
                                </Canvas.Resources>

                                <Ellipse x:Name="ellipse0" Canvas.Left="1.75" Canvas.Top="21"/>
                                <Ellipse x:Name="ellipse1" Canvas.Top="7" Canvas.Left="6.5"/>
                                <Ellipse x:Name="ellipse2" Canvas.Left="20.5" Canvas.Top="0.75"/>
                                <Ellipse x:Name="ellipse3" Canvas.Left="34.75" Canvas.Top="6.75"/>
                                <Ellipse x:Name="ellipse4" Canvas.Left="40.5" Canvas.Top="20.75" />
                                <Ellipse x:Name="ellipse5" Canvas.Left="34.75" Canvas.Top="34.5"/>
                                <Ellipse x:Name="ellipse6" Canvas.Left="20.75" Canvas.Top="39.75"/>
                                <Ellipse x:Name="ellipse7" Canvas.Top="34.25" Canvas.Left="7" />
                                <Ellipse Width="39.5" Height="39.5" Canvas.Left="8.75" Canvas.Top="8" Visibility="Hidden"/>

                                <Label Content="{TemplateBinding MessageText}" 
                                    FontSize="17"
                                    Canvas.Left="60.5" Canvas.Top="11.5"
                                    HorizontalContentAlignment="Center" 
                                    VerticalAlignment="Center"/>

                            </Canvas>
                        </Grid>
                    </Border>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

MyMainWindow.xaml.cs(应用程序启动的主窗口)

using System.ComponentModel;
using System.Windows;

namespace StackHelp
{
    public partial class MyMainWindow : Window, INotifyPropertyChanged
    {
        public MyMainWindow()
        {
            DataContext = this;
            InitializeComponent();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }


        private string _waitMessageText_1 = "Hi there! I am message 1";
        public string WaitMessageText_1
        {
            get
            {
                return _waitMessageText_1;
            }

            set
            {
                if (_waitMessageText_1 == value) return;
                _waitMessageText_1 = value;

                OnPropertyChanged("WaitMessageText_1");
            }
        }

        private string _waitMessageText_2 = "Hi there! I am message 2";
        public string WaitMessageText_2
        {
            get
            {
                return _waitMessageText_2;
            }

            set
            {
                if (_waitMessageText_2 == value) return;
                _waitMessageText_2 = value;

                OnPropertyChanged("WaitMessageText_2");
            }
        }
    }
}

MyMainWindow.xaml(主窗口的实际xaml) 在这里,我有两个实例,一个使用BASIC样式,第二个使用完全定义/旋转样式。

<Window x:Class="StackHelp.MyMainWindow"
    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:myC="clr-namespace:StackHelp.MyClasses"
    Title="My Main Window" Height="250" Width="800">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>

        <!-- Instance 1 using the BASIC style version-->
        <myC:UCBusy MessageText="{Binding WaitMessageText_1}" 
            Grid.Column="0" Style="{StaticResource BusyAnimationStyleBasic}" />

        <!-- Instance 2 using the FINAL style version-->
        <myC:UCBusy MessageText="{Binding WaitMessageText_2}" 
            Grid.Column="1" Style="{StaticResource BusyAnimationStyle}"/>

    </Grid>
</Window>

希望这可以帮助您开始使用自己的样式和实现。