码迷,mamicode.com
首页 > 其他好文 > 详细

自定义BusyIndicator控件

时间:2015-12-15 08:58:07      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

1. style

<Style TargetType="local:CustomBusyIndicator">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="SkyBlue"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomBusyIndicator">
<Border x:Name="PART_Border"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<Grid>
<ContentPresenter x:Name="PART_ContentPresenter"
Focusable="False"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<Grid x:Name="PART_BusyIndicator"
Focusable="False"
Background="#88FFFFFF"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<StackPanel Orientation="Vertical"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock Text="{TemplateBinding BusyContent}"
Foreground="Red"
FontSize="30"/>
<Button x:Name="PART_CancelButton"
HorizontalAlignment="Center"
Content="Cancel"/>
</StackPanel>
</Grid>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsBusy" Value="false">
<Setter Property="Visibility"
TargetName="PART_BusyIndicator"
Value="Collapsed"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

 

2. cs

public class CustomBusyIndicator : Control
{
private const string PART_CancelButton = "PART_CancelButton";

private Button innerCancelButton;

public event RoutedEventHandler CancelClick;

static CustomBusyIndicator()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomBusyIndicator), new FrameworkPropertyMetadata(typeof(CustomBusyIndicator)));
}

#region BusyContent

public string BusyContent
{
get { return (string)GetValue(BusyContentProperty); }
set { SetValue(BusyContentProperty, value); }
}

public static readonly DependencyProperty BusyContentProperty =
DependencyProperty.Register("BusyContent",
typeof(string),
typeof(CustomBusyIndicator),
new PropertyMetadata(""));

#endregion

#region IsBusy

public bool IsBusy
{
get { return (bool)GetValue(IsBusyProperty); }
set { SetValue(IsBusyProperty, value); }
}

public static readonly DependencyProperty IsBusyProperty =
DependencyProperty.Register("IsBusy",
typeof(bool),
typeof(CustomBusyIndicator),
new PropertyMetadata(false));

#endregion

#region Content

public object Content
{
get { return (object)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}

public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register("Content",
typeof(object),
typeof(CustomBusyIndicator),
new PropertyMetadata(null));

#endregion

#region ContentTemplate

public DataTemplate ContentTemplate
{
get { return (DataTemplate)GetValue(ContentTemplateProperty); }
set { SetValue(ContentTemplateProperty, value); }
}

public static readonly DependencyProperty ContentTemplateProperty =
DependencyProperty.Register("ContentTemplate",
typeof(DataTemplate),
typeof(CustomBusyIndicator),
new PropertyMetadata(null));

#endregion

public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (innerCancelButton != null)
{
innerCancelButton.Click -= InnerCancelButton_Click;
}
innerCancelButton = this.GetTemplateChild(PART_CancelButton) as Button;
if (innerCancelButton != null)
{
innerCancelButton.Click += InnerCancelButton_Click;
}
}

private void InnerCancelButton_Click(object sender, RoutedEventArgs e)
{
if (this.IsBusy)
{
this.IsBusy = false;
}
if (this.CancelClick != null)
{
this.CancelClick(this, e);
}
}
}

自定义BusyIndicator控件

标签:

原文地址:http://www.cnblogs.com/iekwei/p/5047052.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!