aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.SharedUI
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/Tango.SharedUI')
-rw-r--r--Software/Visual_Studio/Tango.SharedUI/Components/LazyBinding.cs160
-rw-r--r--Software/Visual_Studio/Tango.SharedUI/Controls/FastTextBlock.cs18
-rw-r--r--Software/Visual_Studio/Tango.SharedUI/Tango.SharedUI.csproj3
3 files changed, 180 insertions, 1 deletions
diff --git a/Software/Visual_Studio/Tango.SharedUI/Components/LazyBinding.cs b/Software/Visual_Studio/Tango.SharedUI/Components/LazyBinding.cs
new file mode 100644
index 000000000..e8e22d182
--- /dev/null
+++ b/Software/Visual_Studio/Tango.SharedUI/Components/LazyBinding.cs
@@ -0,0 +1,160 @@
+using System;
+using System.ComponentModel;
+using System.Globalization;
+using System.Windows;
+using System.Windows.Data;
+using System.Windows.Markup;
+
+namespace Tango.SharedUI.Components
+{
+ [MarkupExtensionReturnType(typeof(object))]
+ public class LazyBindingExtension : MarkupExtension
+ {
+ public LazyBindingExtension()
+ { }
+
+ public LazyBindingExtension(PropertyPath path) : this()
+ {
+ Path = path;
+ }
+
+ #region Properties
+
+ public IValueConverter Converter { get; set; }
+ [TypeConverter(typeof(CultureInfoIetfLanguageTagConverter))]
+ public CultureInfo ConverterCulture { get; set; }
+ public object ConverterParamter { get; set; }
+ public string ElementName { get; set; }
+ [ConstructorArgument("path")]
+ public PropertyPath Path { get; set; }
+ public RelativeSource RelativeSource { get; set; }
+ public object Source { get; set; }
+ public UpdateSourceTrigger UpdateSourceTrigger { get; set; }
+ public bool ValidatesOnDataErrors { get; set; }
+ public bool ValidatesOnExceptions { get; set; }
+ public bool ValidatesOnNotifyDataErrors { get; set; }
+
+ private Binding binding;
+ private UIElement bindingTarget;
+ private DependencyProperty bindingTargetProperty;
+
+ #endregion
+
+ #region Init
+
+ public override object ProvideValue(IServiceProvider serviceProvider)
+ {
+ var valueProvider = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
+ if (valueProvider != null)
+ {
+ bindingTarget = valueProvider.TargetObject as UIElement;
+
+ if (bindingTarget == null)
+ {
+ throw new NotSupportedException($"Target '{valueProvider.TargetObject}' is not valid for a LazyBinding. The LazyBinding target must be a UIElement.");
+ }
+
+ bindingTargetProperty = valueProvider.TargetProperty as DependencyProperty;
+
+ if (bindingTargetProperty == null)
+ {
+ throw new NotSupportedException($"The property '{valueProvider.TargetProperty}' is not valid for a LazyBinding. The LazyBinding target property must be a DependencyProperty.");
+ }
+
+ binding = new Binding
+ {
+ Path = Path,
+ Converter = Converter,
+ ConverterCulture = ConverterCulture,
+ ConverterParameter = ConverterParamter
+ };
+
+ if (ElementName != null)
+ {
+ binding.ElementName = ElementName;
+ }
+
+ if (RelativeSource != null)
+ {
+ binding.RelativeSource = RelativeSource;
+ }
+
+ if (Source != null)
+ {
+ binding.Source = Source;
+ }
+
+ binding.UpdateSourceTrigger = UpdateSourceTrigger;
+ binding.ValidatesOnDataErrors = ValidatesOnDataErrors;
+ binding.ValidatesOnExceptions = ValidatesOnExceptions;
+ binding.ValidatesOnNotifyDataErrors = ValidatesOnNotifyDataErrors;
+
+ return SetBinding();
+ }
+
+ return null;
+ }
+
+ public object SetBinding()
+ {
+ bindingTarget.IsVisibleChanged += UiElement_IsVisibleChanged;
+
+ updateBinding();
+
+ return bindingTarget.GetValue(bindingTargetProperty);
+ }
+
+ #endregion
+
+ #region Event Handlers
+
+ private void UiElement_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
+ {
+ updateBinding();
+ }
+
+ #endregion
+
+ #region Update Binding
+
+ private void updateBinding()
+ {
+ if (bindingTarget.IsVisible)
+ {
+ ConsolidateBinding();
+ }
+ else
+ {
+ ClearBinding();
+ }
+ }
+
+ private bool _isBind;
+
+ private void ConsolidateBinding()
+ {
+ if (_isBind)
+ {
+ return;
+ }
+
+ _isBind = true;
+
+ BindingOperations.SetBinding(bindingTarget, bindingTargetProperty, binding);
+ }
+
+ private void ClearBinding()
+ {
+ if (!_isBind)
+ {
+ return;
+ }
+
+ BindingOperations.ClearBinding(bindingTarget, bindingTargetProperty);
+
+ _isBind = false;
+ }
+
+ #endregion
+ }
+}
diff --git a/Software/Visual_Studio/Tango.SharedUI/Controls/FastTextBlock.cs b/Software/Visual_Studio/Tango.SharedUI/Controls/FastTextBlock.cs
index 141fa6e27..07005a0f4 100644
--- a/Software/Visual_Studio/Tango.SharedUI/Controls/FastTextBlock.cs
+++ b/Software/Visual_Studio/Tango.SharedUI/Controls/FastTextBlock.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
@@ -30,6 +31,23 @@ namespace Tango.SharedUI.Controls
public FastTextBlock()
{
Loaded += FastTextBlock_Loaded;
+
+ DependencyPropertyDescriptor fontFamilyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(FastTextBlock.FontFamilyProperty, typeof(FastTextBlock));
+ fontFamilyPropertyDescriptor.AddValueChanged(this, OnPropertyChangedInit);
+
+ DependencyPropertyDescriptor fontSizePropertyDescriptor = DependencyPropertyDescriptor.FromProperty(FastTextBlock.FontSizeProperty, typeof(FastTextBlock));
+ fontSizePropertyDescriptor.AddValueChanged(this, OnPropertyChangedInit);
+
+ DependencyPropertyDescriptor fontWeightPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(FastTextBlock.FontWeightProperty, typeof(FastTextBlock));
+ fontWeightPropertyDescriptor.AddValueChanged(this, OnPropertyChangedInit);
+
+ DependencyPropertyDescriptor ForegroundPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(FastTextBlock.ForegroundProperty, typeof(FastTextBlock));
+ ForegroundPropertyDescriptor.AddValueChanged(this, OnPropertyChangedInit);
+ }
+
+ private void OnPropertyChangedInit(object sender, EventArgs e)
+ {
+ Init();
}
private void FastTextBlock_Loaded(object sender, RoutedEventArgs e)
diff --git a/Software/Visual_Studio/Tango.SharedUI/Tango.SharedUI.csproj b/Software/Visual_Studio/Tango.SharedUI/Tango.SharedUI.csproj
index e4dc72746..5d9592a26 100644
--- a/Software/Visual_Studio/Tango.SharedUI/Tango.SharedUI.csproj
+++ b/Software/Visual_Studio/Tango.SharedUI/Tango.SharedUI.csproj
@@ -67,6 +67,7 @@
<Compile Include="Binding\BindingEventArgs.cs" />
<Compile Include="Binding\BindingEventContainer.cs" />
<Compile Include="Binding\BindingProperty.cs" />
+ <Compile Include="Components\LazyBinding.cs" />
<Compile Include="Components\BindingProxy.cs" />
<Compile Include="Components\SelectedObject.cs" />
<Compile Include="Components\SelectedObjectCollection.cs" />
@@ -261,7 +262,7 @@
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
- <UserProperties BuildVersion_StartDate="2000/1/1" BuildVersion_UseGlobalSettings="False" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" />
+ <UserProperties BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UseGlobalSettings="False" BuildVersion_StartDate="2000/1/1" />
</VisualStudio>
</ProjectExtensions>
</Project> \ No newline at end of file