因为项目中Syncfusion的BusyIndicator控件存在bug,所以实现一个较为简单的自定义BusyIndicator。
1. 创建Style
1 <Style TargetType="local:CustomBusyIndicator"> 2 <Setter Property="Background" Value="White"/> 3 <Setter Property="Foreground" Value="SkyBlue"/> 4 <Setter Property="BorderThickness" Value="2"/> 5 <Setter Property="BorderBrush" Value="Red"/> 6 <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 7 <Setter Property="VerticalContentAlignment" Value="Stretch"/> 8 <Setter Property="Padding" Value="1"/> 9 <Setter Property="Template"> 10 <Setter.Value> 11 <ControlTemplate TargetType="local:CustomBusyIndicator"> 12 <Border x:Name="PART_Border" 13 BorderBrush="{TemplateBinding BorderBrush}" 14 BorderThickness="{TemplateBinding BorderThickness}" 15 Background="{TemplateBinding Background}" 16 SnapsToDevicePixels="true"> 17 <Grid> 18 <ContentPresenter x:Name="PART_ContentPresenter" 19 Focusable="False" 20 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 21 Margin="{TemplateBinding Padding}" 22 RecognizesAccessKey="True" 23 Content="{TemplateBinding Content}" 24 ContentTemplate="{TemplateBinding ContentTemplate}" 25 SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 26 VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 27 <Grid x:Name="PART_BusyIndicator" 28 Focusable="False" 29 Background="#88FFFFFF" 30 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 31 SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 32 VerticalAlignment="{TemplateBinding VerticalContentAlignment}"> 33 <StackPanel Orientation="Vertical" 34 HorizontalAlignment="Center" 35 VerticalAlignment="Center"> 36 <TextBlock Text="{TemplateBinding BusyContent}" 37 Foreground="Red" 38 FontSize="30"/> 39 <Button x:Name="PART_CancelButton" 40 HorizontalAlignment="Center" 41 Content="Cancel"/> 42 </StackPanel> 43 </Grid> 44 </Grid> 45 </Border> 46 <ControlTemplate.Triggers> 47 <Trigger Property="IsBusy" Value="false"> 48 <Setter Property="Visibility" 49 TargetName="PART_BusyIndicator" 50 Value="Collapsed"/> 51 </Trigger> 52 </ControlTemplate.Triggers> 53 </ControlTemplate> 54 </Setter.Value> 55 </Setter> 56 </Style>
2. 创建cs
1 public class CustomBusyIndicator : Control 2 { 3 private const string PART_CancelButton = "PART_CancelButton"; 4 5 private Button innerCancelButton; 6 7 public event RoutedEventHandler CancelClick; 8 9 static CustomBusyIndicator() 10 { 11 DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomBusyIndicator), new FrameworkPropertyMetadata(typeof(CustomBusyIndicator))); 12 } 13 14 #region BusyContent 15 16 public string BusyContent 17 { 18 get { return (string)GetValue(BusyContentProperty); } 19 set { SetValue(BusyContentProperty, value); } 20 } 21 22 public static readonly DependencyProperty BusyContentProperty = 23 DependencyProperty.Register("BusyContent", 24 typeof(string), 25 typeof(CustomBusyIndicator), 26 new PropertyMetadata("")); 27 28 #endregion 29 30 #region IsBusy 31 32 public bool IsBusy 33 { 34 get { return (bool)GetValue(IsBusyProperty); } 35 set { SetValue(IsBusyProperty, value); } 36 } 37 38 public static readonly DependencyProperty IsBusyProperty = 39 DependencyProperty.Register("IsBusy", 40 typeof(bool), 41 typeof(CustomBusyIndicator), 42 new PropertyMetadata(false)); 43 44 #endregion 45 46 #region Content 47 48 public object Content 49 { 50 get { return (object)GetValue(ContentProperty); } 51 set { SetValue(ContentProperty, value); } 52 } 53 54 public static readonly DependencyProperty ContentProperty = 55 DependencyProperty.Register("Content", 56 typeof(object), 57 typeof(CustomBusyIndicator), 58 new PropertyMetadata(null)); 59 60 #endregion 61 62 #region ContentTemplate 63 64 public DataTemplate ContentTemplate 65 { 66 get { return (DataTemplate)GetValue(ContentTemplateProperty); } 67 set { SetValue(ContentTemplateProperty, value); } 68 } 69 70 public static readonly DependencyProperty ContentTemplateProperty = 71 DependencyProperty.Register("ContentTemplate", 72 typeof(DataTemplate), 73 typeof(CustomBusyIndicator), 74 new PropertyMetadata(null)); 75 76 #endregion 77 78 public override void OnApplyTemplate() 79 { 80 base.OnApplyTemplate(); 81 if (innerCancelButton != null) 82 { 83 innerCancelButton.Click -= InnerCancelButton_Click; 84 } 85 innerCancelButton = this.GetTemplateChild(PART_CancelButton) as Button; 86 if (innerCancelButton != null) 87 { 88 innerCancelButton.Click += InnerCancelButton_Click; 89 } 90 } 91 92 private void InnerCancelButton_Click(object sender, RoutedEventArgs e) 93 { 94 if (this.IsBusy) 95 { 96 this.IsBusy = false; 97 } 98 if (this.CancelClick != null) 99 { 100 this.CancelClick(this, e); 101 } 102 } 103 }
浙公网安备 33010602011771号