Skip to content

Instantly share code, notes, and snippets.

@seank-com
Last active October 12, 2015 15:47
Show Gist options
  • Save seank-com/4049775 to your computer and use it in GitHub Desktop.
Save seank-com/4049775 to your computer and use it in GitHub Desktop.
Xaml Attached Dependency Property
// 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