Hi,
You can do something like this :
XAML
<Window x:Class="WpfSOTest.BusyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfSOTest"
Title="BusyWindow"
Height="300"
Width="300">
<Window.Resources>
<local:VisibilityConverter x:Key="VisibilityConverter" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border Grid.Row="0">
<Grid>
<Border>
<Rectangle Width="400"
Height="400"
Fill="#EEE" />
</Border>
<Border Visibility="{Binding IsBusy, Converter={StaticResource VisibilityConverter}}">
<Grid>
<Rectangle Width="400"
Height="400"
Fill="#AAA" />
<TextBlock Text="Busy"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
</Border>
</Grid>
</Border>
<Border Grid.Row="1">
<Button Click="ChangeVisualState">Change Visual State</Button>
</Border>
</Grid>
Code:
public partial class BusyWindow : Window
{
ViewModel viewModel = new ViewModel();
public BusyWindow()
{
InitializeComponent();
DataContext = viewModel;
}
private void ChangeVisualState(object sender, RoutedEventArgs e)
{
viewModel.IsBusy = !viewModel.IsBusy;
}
}
public class ViewModel : INotifyPropertyChanged
{
protected Boolean _isBusy;
public Boolean IsBusy
{
get { return _isBusy; }
set { _isBusy = value; RaisePropertyChanged("IsBusy"); }
}
public ViewModel()
{
IsBusy = false;
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(String propertyName)
{
PropertyChangedEventHandler temp = PropertyChanged;
if (temp != null)
{
temp(this, new PropertyChangedEventArgs(propertyName));
}
}
}
class VisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch (((Boolean)value))
{
case true:
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
----------------------- Updated code -----------------------
XAML
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border Grid.Row="0">
<Grid>
<local:MyBorder IsBusy="{Binding IsBusy}">
<Grid>
<TextBlock Text="{Binding IsBusy}"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
</local:MyBorder>
</Grid>
</Border>
<Border Grid.Row="1">
<Button Click="ChangeVisualState">Change Visual State</Button>
</Border>
</Grid>
Template
<Style TargetType="{x:Type local:MyBorder}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyBorder}">
<Border Name="RootBorder">
<Border.Background>
<SolidColorBrush x:Name="NormalBrush"
Color="Transparent" />
</Border.Background>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="CommonGroups">
<VisualState Name="Normal" />
</VisualStateGroup>
<VisualStateGroup Name="CustomGroups">
<VisualState Name="Busy">
<VisualState.Storyboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="NormalBrush"
Storyboard.TargetProperty="Color"
Duration="0:0:0.5"
AutoReverse="True"
RepeatBehavior="Forever"
To="#EEE" />
</Storyboard>
</VisualState.Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Custom Element
[TemplateVisualState(GroupName = "CustomGroups", Name = "Busy")]
public class MyBorder : ContentControl
{
static MyBorder()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyBorder), new FrameworkPropertyMetadata(typeof(MyBorder)));
}
public Boolean IsBusy
{
get { return (Boolean)GetValue(IsBusyProperty); }
set { SetValue(IsBusyProperty, value); }
}
public static readonly DependencyProperty IsBusyProperty =
DependencyProperty.Register("IsBusy", typeof(Boolean), typeof(MyBorder), new UIPropertyMetadata(IsBusyPropertyChangedCallback));
static void IsBusyPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as MyBorder).OnIsBusyPropertyChanged(d, e);
}
private void OnIsBusyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (Convert.ToBoolean(e.NewValue))
{
VisualStateManager.GoToState(this, "Busy", true);
}
else
{
VisualStateManager.GoToState(this, "Normal", true);
}
}
}