forked from snoopwpf/snoopwpf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BringIntoViewBehavior.cs
58 lines (54 loc) · 1.29 KB
/
BringIntoViewBehavior.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// (c) Copyright Cory Plotts.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http:https://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using System;
using System.Windows;
using Snoop.Infrastructure;
namespace Snoop
{
/// <summary>
/// Exposes attached behaviors that brings given FrameworkElement into view.
/// </summary>
public static class BringIntoViewBehavior
{
public static bool GetIsActive(FrameworkElement fe)
{
return (bool)fe.GetValue(IsActiveProperty);
}
public static void SetIsActive(FrameworkElement fe, bool value)
{
fe.SetValue(IsActiveProperty, value);
}
public static readonly DependencyProperty IsActiveProperty =
DependencyProperty.RegisterAttached
(
"IsActive",
typeof(bool),
typeof(BringIntoViewBehavior),
new UIPropertyMetadata(false, OnIsActiveChanged)
);
private static void OnIsActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var fe = d as FrameworkElement;
if (fe == null)
{
return;
}
if ((bool)e.NewValue)
{
// Can Bring into view only when element is loaded.
fe.WhenLoaded
(
element =>
{
if (GetIsActive(element))
{
element.BringIntoView();
}
}
);
}
}
}
}