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

Reusing WPF user control by parametrizing it does not work

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

从我的 WPF 主 Window 的不同区域,我正在创建这个用户控件的不同实例,所以我想参数化这个用户控件以便重用它并在每个标签中显示不同的消息是时候我创建它的一个新实例了。为了实现这一点,我在名为 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 始终相同(我已将其设置为指向视图模型)因此每次创建用户控件的新实例时无需更改它。

我希望您先看一下我提供的先前答案,该答案对自定义控件、属性、样式等做了很多步骤。

几个直接问题...您的 "Style" 声明基于 "Control",但您的 class 是基于 "Control",但您的自定义 class 明确是一个 "UCBusy" 控件,所以 "Control" 不知道 "MessageText" 是什么样式绑定。

此外,您部分通过了它,因为您声明为不同的 class 类型。

根据我之前的建议使用完整样本进行了编辑。 这是可以查看所有部分的源代码。我创建了一个新项目 "StackHelp" 并具有以下内容。请注意,合并的字典指的是 "component/MyClasses…" 子文件夹,我的自定义 classes 和相应的 .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 特别是 RESOURCE DICTIONARY 项) 定义了两种样式...一种基本样式,就像一个独立标签,只是为了查看绑定是如何完成的...第二种样式专门用于制作您精心设计的样式。 "xmlns:myC" 表示 "myC" 别名指的是在每个命名空间的项目的 "MyClasses" 子文件夹中找到的 classes。

<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(应用程序启动的主要window)

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(主window的实际xaml) 在这里,我有两个实例,一个使用 BASIC 样式,第二个使用完全 defined/spinner 样式。

<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>

希望这完成后,您可以快速开始自己的样式和实现。