Last active
October 12, 2015 15:47
-
-
Save seank-com/4049775 to your computer and use it in GitHub Desktop.
Xaml Attached Dependency Property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage in Xaml | |
// | |
// <FlipView UpdateOnChange.VisualState="{Binding Path=VisualState}"> | |
// | |
// | |
// Header File | |
#pragma once | |
namespace FlipIt | |
{ | |
public ref class UpdateOnChange sealed | |
{ | |
public: | |
static property Windows::UI::Xaml::DependencyProperty^ VisualStateProperty | |
{ | |
Windows::UI::Xaml::DependencyProperty^ get(); | |
} | |
static Platform::String^ GetVisualState(Windows::UI::Xaml::DependencyObject^ obj); | |
static void SetVisualState(Windows::UI::Xaml::DependencyObject^ obj, Platform::String^ value); | |
static void OnVisualStateChanged(Windows::UI::Xaml::DependencyObject^ object, Windows::UI::Xaml::DependencyPropertyChangedEventArgs^ e); | |
private: | |
static Windows::UI::Xaml::DependencyProperty^ s_visualState; | |
}; | |
} | |
// Code file | |
#include "pch.h" | |
#include "UpdateOnChange.h" | |
using namespace FlipIt; | |
using namespace std; | |
using namespace Platform; | |
using namespace Windows::Foundation; | |
using namespace Windows::UI::Xaml; | |
using namespace Windows::UI::Xaml::Controls; | |
using namespace Windows::UI::Xaml::Interop; | |
DependencyProperty^ UpdateOnChange::s_visualState = DependencyProperty::RegisterAttached( | |
L"VisualState", | |
String::typeid, | |
UpdateOnChange::typeid, | |
ref new PropertyMetadata(nullptr, ref new PropertyChangedCallback(&UpdateOnChange::OnVisualStateChanged))); | |
DependencyProperty^ UpdateOnChange::VisualStateProperty::get() | |
{ | |
return s_visualState; | |
} | |
String^ UpdateOnChange::GetVisualState(Windows::UI::Xaml::DependencyObject^ obj) | |
{ | |
return safe_cast<String^>(obj->GetValue(s_visualState)); | |
} | |
void UpdateOnChange::SetVisualState(Windows::UI::Xaml::DependencyObject^ obj, Platform::String^ value) | |
{ | |
obj->SetValue(s_visualState, value); | |
} | |
void UpdateOnChange::OnVisualStateChanged(DependencyObject^ d, DependencyPropertyChangedEventArgs^ e) | |
{ | |
String^ oldValue = safe_cast<String^>(e->OldValue); | |
String^ newValue = safe_cast<String^>(e->NewValue); | |
if (oldValue != newValue) | |
{ | |
if (newValue != nullptr && newValue != "") | |
{ | |
ContentControl^ cc = safe_cast<ContentControl^>(d); | |
VisualStateManager::GoToState(cc, newValue, false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment