how add behavior in expression blend - c#

Hi
There is a behavior in following code
I want to use this behavior in my WPF application
But i cant attached this behavior to my project
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.ComponentModel;
namespace Brainsiders.MouseOver3D
{
[Description("MouseOver3D - A behavior that applies the MouseOver Design Interaction Pattern to a 3D environment.")]
public class MouseOver3D : TargetedTriggerAction<FrameworkElement>
{
private TimeSpan hoverUp_duration = TimeSpan.FromSeconds(0.5);
[Category("Mouse Over 3D - Going Up")]
public TimeSpan HoverUp_duration
{
get { return hoverUp_duration; }
set { hoverUp_duration = value; }
}
private TimeSpan hoverDown_Duration = TimeSpan.FromSeconds(0.9);
[Category("Mouse Over 3D - Going Down")]
public TimeSpan HoverDown_Duration
{
get { return hoverDown_Duration; }
set { hoverDown_Duration = value; }
}
[Category("Mouse Over 3D - Going Up")]
public IEasingFunction HoverUp_Easing { get; set; }
[Category("Mouse Over 3D - Going Down")]
public IEasingFunction HoverDown_Easing { get; set; }
private double hoverOffset = 30;
[Category("Mouse Over 3D - General")]
public double HoverOffset
{
get { return hoverOffset; }
set { hoverOffset = value; }
}
private FrameworkElement feAssociatedObject;
private FrameworkElement feSourceObject;
private FrameworkElement feTargetObject;
private PlaneProjection ProjectionTargetObject;
private Storyboard SB_HoverZ;
protected override void Invoke(object parameter)
{
FrameworkElement myElement = this.AssociatedObject as FrameworkElement;
}
protected override void OnAttached()
{
base.OnAttached();
feAssociatedObject = (FrameworkElement)this.AssociatedObject;
feSourceObject = (FrameworkElement)this.AssociatedObject;
feSourceObject.Loaded += new RoutedEventHandler(feSourceObject_Loaded);
}
void feSourceObject_Loaded(object sender, RoutedEventArgs e)
{
feSourceObject.Loaded -= new RoutedEventHandler(feSourceObject_Loaded);
ProjectionTargetObject = new PlaneProjection();
feTargetObject = (FrameworkElement)this.TargetObject;
if (feTargetObject == null) feTargetObject = feAssociatedObject;
if (feTargetObject.Projection == null)
{
feTargetObject.RenderTransformOrigin = new Point(0.5, 0.5);
PlaneProjection pj = new PlaneProjection();
feTargetObject.Projection = pj;
}
feSourceObject.MouseEnter += new MouseEventHandler(feSourceObject_MouseEnter);
feSourceObject.MouseLeave += new MouseEventHandler(feSourceObject_MouseLeave);
}
protected override void OnDetaching()
{
base.OnDetaching();
}
void feSourceObject_MouseLeave(object sender, MouseEventArgs e)
{
DeactivateAnimation();
}
void feSourceObject_MouseEnter(object sender, MouseEventArgs e)
{
ActivateAnimation();
}
bool bAnimationActivated = false;
private void ActivateAnimation()
{
if (bAnimationActivated == false)
{
AnimateHoverZ(HoverOffset, true);
bAnimationActivated = true;
}
}
private void DeactivateAnimation()
{
if (bAnimationActivated == true)
{
AnimateHoverZ(0, false);
bAnimationActivated = false;
}
}
private void AnimateHoverZ( Double Z, bool HoverUp)
{
if (HoverUp == true)
playAnimation(feTargetObject, "(UIElement.Projection).(PlaneProjection.LocalOffsetZ)", HoverUp_duration, Z, SB_HoverZ, HoverUp_Easing);
else
playAnimation(feTargetObject, "(UIElement.Projection).(PlaneProjection.LocalOffsetZ)", HoverDown_Duration, Z, SB_HoverZ, HoverDown_Easing);
}
public static void playAnimation(FrameworkElement element, string property, TimeSpan time, double value, Storyboard sb, IEasingFunction EasingFunction)
{
sb = new Storyboard();
sb.Children.Clear();
DoubleAnimation animation = new DoubleAnimation();
animation.Duration = time;
animation.To = value;
animation.EasingFunction = EasingFunction;
Storyboard.SetTargetProperty(animation, new PropertyPath(property));
Storyboard.SetTarget(animation, element);
sb.Children.Add(animation);
sb.Begin();
}
}
}

You need to add the necessary references (System.Windows.Interactivity)
to your project, for that you need to have some version of the Blend SDK installed.
Then to use this in XAML you need to define namespaces for both interactivity and this behavior:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:bs="clr-namespace:Brainsiders.MouseOver3D"
Then you should be able to attach it like this:
<Button>
<i:Interaction.Behaviors>
<bs:MouseOver3D />
</i:Interaction.Behaviors>
</Button>

Related

Custom Picker Xamarin Android

I have a picker.Please,tell me how to change color of title, the color of items and remove these lines
I have my custompicker and I could change colors of buttons CANCEL,OK
I tried to remove lines
https://forums.xamarin.com/discussion/78693/how-can-i-remove-the-picker-borders-in-forms-for-android
but it does not work
How to change color of SELECT A CAR and AUDI I do not know
You could implement it by using Custom Renderer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using App12.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Color = Android.Graphics.Color;
using Orientation = Android.Widget.Orientation;
[assembly: ExportRenderer(typeof(Picker), typeof(MyPickerRenderer))]
namespace App12.Droid
{
public class MyPickerRenderer:PickerRenderer
{
IElementController ElementController => Element;
public MyPickerRenderer(Context context) : base(context)
{
}
private AlertDialog _dialog;
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
Control.Click += Control_Click;
Control.SetHintTextColor(Android.Graphics.Color.Red);
Control.SetSingleLine(true);
Control.SetTypeface(null, TypefaceStyle.Bold);
Control.Gravity = GravityFlags.Center;
var gd = new GradientDrawable();
gd.SetStroke(0, Android.Graphics.Color.Transparent);
Control.SetBackground(gd);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
Control.Click -= Control_Click;
//var picker = (Picker)Element;
//picker.PropertyChanged -= Control_Click;
}
base.Dispose(disposing);
}
private void Control_Click(object sender, EventArgs e)
{
Picker model = Element;
picker.SelectionDividerHeight = 0;
var picker = new TextColorNumberPicker(Context);
if (model.Items != null && model.Items.Any())
{
// set style here
picker.MaxValue = model.Items.Count - 1;
picker.MinValue = 0;
picker.SetDisplayedValues(model.Items.ToArray());
picker.WrapSelectorWheel = false;
picker.Value = model.SelectedIndex;
}
var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
layout.AddView(picker);
var titleView = new TextView(Context);
titleView.Text = "Select a car";
titleView.TextSize = 20;
titleView.SetTextColor(Color.Red);
titleView.SetBackgroundColor(Color.White);
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);
var builder = new AlertDialog.Builder(Context);
builder.SetView(layout);
builder.SetTitle(model.Title ?? "");
builder.SetCustomTitle(titleView);
builder.SetNegativeButton("Cancel ", (s, a) =>
{
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
// It is possible for the Content of the Page to be changed when Focus is changed.
// In this case, we'll lose our Control.
Control?.ClearFocus();
_dialog = null;
});
builder.SetPositiveButton("Ok ", (s, a) =>
{
ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, picker.Value);
// It is possible for the Content of the Page to be changed on SelectedIndexChanged.
// In this case, the Element & Control will no longer exist.
if (Element != null)
{
if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
Control.Text = model.Items[Element.SelectedIndex];
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
// It is also possible for the Content of the Page to be changed when Focus is changed.
// In this case, we'll lose our Control.
Control?.ClearFocus();
}
_dialog = null;
});
_dialog = builder.Create();
_dialog.DismissEvent += (ssender, args) =>
{
ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
};
_dialog.Show();
Android.Widget.Button btnOk = _dialog.GetButton((int)Android.Content.DialogButtonType.Positive);
btnOk.SetTextColor(Android.Graphics.Color.Blue);
Android.Widget.Button btnCancel = _dialog.GetButton((int)Android.Content.DialogButtonType.Positive);
btnCancel.SetTextColor(Android.Graphics.Color.Gray);
}
}
public class TextColorNumberPicker : NumberPicker
{
public TextColorNumberPicker(Context context) : base(context)
{
}
public override void AddView(Android.Views.View child, int index, ViewGroup.LayoutParams #params)
{
base.AddView(child, index, #params);
UpdateView(child);
}
public void UpdateView(Android.Views.View view)
{
if (view is EditText)
{
((EditText)view).SetTextColor(Color.Red); // set item text color
}
}
}
}
Note : Make sure the Target Framework of the project is the latest stable version (Android Q) .
Override Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer instead of Xamarin.Forms.Platform.Android.PickerRenderer
public class CustomPickerRenderer
: Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer
{
...
}
Reference: https://www.damirscorner.com/blog/posts/20201204-CustomPickerRendererOnAndroid.html

How can I Use Connected Animation in WPF App like UWP

I want to implement UWP connected animation in my WPF app. like this......
Is it possible? If it is then how can I do it?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Walterlv.Annotations;
namespace Walterlv.Demo.Media.Animation
{
public class ConnectedAnimation
{
internal ConnectedAnimation([NotNull] string key, [NotNull] UIElement source, [NotNull] EventHandler completed)
{
Key = key ?? throw new ArgumentNullException(nameof(key));
_source = source ?? throw new ArgumentNullException(nameof(source));
_reportCompleted = completed ?? throw new ArgumentNullException(nameof(completed));
}
public string Key { get; }
private readonly UIElement _source;
private readonly EventHandler _reportCompleted;
public bool TryStart([NotNull] UIElement destination)
{
return TryStart(destination, Enumerable.Empty<UIElement>());
}
public bool TryStart([NotNull] UIElement destination, [NotNull] IEnumerable<UIElement> coordinatedElements)
{
if (destination == null)
{
throw new ArgumentNullException(nameof(destination));
}
if (coordinatedElements == null)
{
throw new ArgumentNullException(nameof(coordinatedElements));
}
if (Equals(_source, destination))
{
return false;
}
// showing the animation?
// ready to connect the animation。
var adorner = ConnectedAnimationAdorner.FindFrom(destination);
var connectionHost = new ConnectedVisual(_source, destination);
adorner.Children.Add(connectionHost);
var storyboard = new Storyboard();
var animation = new DoubleAnimation(0.0, 1.0, new Duration(TimeSpan.FromSeconds(10.6)))
{
EasingFunction = new CubicEase {EasingMode = EasingMode.EaseInOut},
};
Storyboard.SetTarget(animation, connectionHost);
Storyboard.SetTargetProperty(animation, new PropertyPath(ConnectedVisual.ProgressProperty.Name));
storyboard.Children.Add(animation);
storyboard.Completed += (sender, args) =>
{
_reportCompleted(this, EventArgs.Empty);
//destination.ClearValue(UIElement.VisibilityProperty);
adorner.Children.Remove(connectionHost);
};
//destination.Visibility = Visibility.Hidden;
storyboard.Begin();
return true;
}
private class ConnectedVisual : DrawingVisual
{
public static readonly DependencyProperty ProgressProperty = DependencyProperty.Register(
"Progress", typeof(double), typeof(ConnectedVisual),
new PropertyMetadata(0.0, OnProgressChanged), ValidateProgress);
public double Progress
{
get => (double) GetValue(ProgressProperty);
set => SetValue(ProgressProperty, value);
}
private static bool ValidateProgress(object value) =>
value is double progress && progress >= 0 && progress <= 1;
private static void OnProgressChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ConnectedVisual) d).Render((double) e.NewValue);
}
public ConnectedVisual([NotNull] Visual source, [NotNull] Visual destination)
{
_source = source ?? throw new ArgumentNullException(nameof(source));
_destination = destination ?? throw new ArgumentNullException(nameof(destination));
_sourceBrush = new VisualBrush(source) {Stretch = Stretch.Fill};
_destinationBrush = new VisualBrush(destination) {Stretch = Stretch.Fill};
}
private readonly Visual _source;
private readonly Visual _destination;
private readonly Brush _sourceBrush;
private readonly Brush _destinationBrush;
private Rect _sourceBounds;
private Rect _destinationBounds;
protected override void OnVisualParentChanged(DependencyObject oldParent)
{
if (VisualTreeHelper.GetParent(this) == null)
{
return;
}
var sourceBounds = VisualTreeHelper.GetContentBounds(_source);
if (sourceBounds.IsEmpty)
{
sourceBounds = VisualTreeHelper.GetDescendantBounds(_source);
}
_sourceBounds = new Rect(
_source.PointToScreen(sourceBounds.TopLeft),
_source.PointToScreen(sourceBounds.BottomRight));
_sourceBounds = new Rect(
PointFromScreen(_sourceBounds.TopLeft),
PointFromScreen(_sourceBounds.BottomRight));
var destinationBounds = VisualTreeHelper.GetContentBounds(_destination);
if (destinationBounds.IsEmpty)
{
destinationBounds = VisualTreeHelper.GetDescendantBounds(_destination);
}
_destinationBounds = new Rect(
_destination.PointToScreen(destinationBounds.TopLeft),
_destination.PointToScreen(destinationBounds.BottomRight));
_destinationBounds = new Rect(
PointFromScreen(_destinationBounds.TopLeft),
PointFromScreen(_destinationBounds.BottomRight));
}
private void Render(double progress)
{
var bounds = new Rect(
(_destinationBounds.Left - _sourceBounds.Left) * progress + _sourceBounds.Left,
(_destinationBounds.Top - _sourceBounds.Top) * progress + _sourceBounds.Top,
(_destinationBounds.Width - _sourceBounds.Width) * progress + _sourceBounds.Width,
(_destinationBounds.Height - _sourceBounds.Height) * progress + _sourceBounds.Height);
using (var dc = RenderOpen())
{
dc.DrawRectangle(_sourceBrush, null, bounds);
dc.PushOpacity(progress);
dc.DrawRectangle(_destinationBrush, null, bounds);
dc.Pop();
}
}
}
private class ConnectedAnimationAdorner : Adorner
{
private ConnectedAnimationAdorner([NotNull] UIElement adornedElement)
: base(adornedElement)
{
Children = new VisualCollection(this);
IsHitTestVisible = false;
}
internal VisualCollection Children { get; }
protected override int VisualChildrenCount => Children.Count;
protected override Visual GetVisualChild(int index) => Children[index];
protected override Size ArrangeOverride(Size finalSize)
{
foreach (var child in Children.OfType<UIElement>())
{
child.Arrange(new Rect(child.DesiredSize));
}
return finalSize;
}
internal static ConnectedAnimationAdorner FindFrom([NotNull] Visual visual)
{
if (Window.GetWindow(visual)?.Content is UIElement root)
{
var layer = AdornerLayer.GetAdornerLayer(root);
if (layer != null)
{
var adorner = layer.GetAdorners(root)?.OfType<ConnectedAnimationAdorner>().FirstOrDefault();
if (adorner == null)
{
adorner = new ConnectedAnimationAdorner(root);
layer.Add(adorner);
}
return adorner;
}
}
throw new InvalidOperationException("指定的 Visual 尚未连接到可见的视觉树中,找不到用于承载动画的容器。");
}
internal static void ClearFor([NotNull] Visual visual)
{
if (Window.GetWindow(visual)?.Content is UIElement root)
{
var layer = AdornerLayer.GetAdornerLayer(root);
var adorner = layer?.GetAdorners(root)?.OfType<ConnectedAnimationAdorner>().FirstOrDefault();
if (adorner != null)
{
layer.Remove(adorner);
}
}
}
}
}
}
How to use?
private int index;
private void AnimationButton_Click(object sender, RoutedEventArgs e)
{
BeginConnectedAnimation((UIElement)sender, ConnectionDestination);
}
private async void BeginConnectedAnimation(UIElement source, UIElement destination)
{
var service = ConnectedAnimationService.GetForCurrentView(this);
service.PrepareToAnimate($"Test{index}", source);
var animation = service.GetAnimation($"Test{index}");
animation?.TryStart(destination);
// use different Key when click。
index++;
}
See https://walterlv.com/post/connected-animation-of-wpf.html

name does not exist in the namespace of wpf project

summary of soInput.xaml:
xmlns:core="clr-namespace:MyProject.Fun.Core"
xmlns:behave="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<TextBox Name="txtValue"
Margin="0"
Style="{DynamicResource soTextBox}"
TextWrapping="NoWrap"
Height="50"
Foreground="#FF363636"
FontSize="18.667"
FontWeight="Bold"
Grid.ColumnSpan="2"
IsEnabled="{Binding IsEnabled,ElementName=Input}"
Text="{Binding soValue,ElementName=Input,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
GotFocus="txtValue_GotFocus"
LostFocus="txtValue_LostFocus">
<behave:Interaction.Behaviors>
<core:TextBoxKeyboardBehavior/>
</behave:Interaction.Behaviors>
</TextBox>
soInput.xaml.cs:
namespace MyProject.Fun
{
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
public partial class soInput : UserControl, IComponentConnector
{
public static readonly DependencyProperty soErrorProperty = DependencyProperty.Register("soError", typeof(string), typeof(soInput));
public static readonly DependencyProperty soHelpDescriptionProperty = DependencyProperty.Register("soHelpDescription", typeof(string), typeof(soInput), new UIPropertyMetadata(""));
public static readonly DependencyProperty soHelpTitleProperty = DependencyProperty.Register("soHelpTitle", typeof(string), typeof(soInput), new UIPropertyMetadata(""));
public static readonly DependencyProperty soLabelProperty = DependencyProperty.Register("soLabel", typeof(string), typeof(soInput));
public static readonly DependencyProperty soValueProperty = DependencyProperty.Register("soValue", typeof(string), typeof(soInput));
public soInput()
{
this.InitializeComponent();
}
private void Input_GotFocus(object sender, RoutedEventArgs e)
{
}
private void txtValue_GotFocus(object sender, RoutedEventArgs e)
{
}
private void txtValue_LostFocus(object sender, RoutedEventArgs e)
{
}
public string soError
{
get
{
return (string) base.GetValue(soErrorProperty);
}
set
{
base.SetValue(soErrorProperty, value);
}
}
public string soHelpDescription
{
get
{
return (string) base.GetValue(soHelpDescriptionProperty);
}
set
{
base.SetValue(soHelpDescriptionProperty, value);
}
}
public string soHelpTitle
{
get
{
return (string) base.GetValue(soHelpTitleProperty);
}
set
{
base.SetValue(soHelpTitleProperty, value);
}
}
public string soLabel
{
get
{
return (string) base.GetValue(soLabelProperty);
}
set
{
base.SetValue(soLabelProperty, value);
}
}
public string soValue
{
get
{
return (string) base.GetValue(soValueProperty);
}
set
{
base.SetValue(soValueProperty, value);
}
}
}
}
TextBoxKeyboardBehavior.cs:
using MyProject.Fun.Helpers;
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interactivity;
namespace MyProject.Fun.Core
{
public partial class TextBoxKeyboardBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
base.AssociatedObject.GotFocus += new RoutedEventHandler(this.AssociatedObject_GotFocus);
base.AssociatedObject.LostFocus += new RoutedEventHandler(this.AssociatedObject_LostFocus);
}
private void AssociatedObject_LostFocus(object sender, RoutedEventArgs e)
{
Button button = base.AssociatedObject.Template.FindName("keyboardBtn", base.AssociatedObject) as Button;
if (button != null)
{
button.Click -= new RoutedEventHandler(this.btn_Click);
}
if (button != null)
{
button.Visibility = Visibility.Hidden;
}
}
private void AssociatedObject_GotFocus(object sender, RoutedEventArgs e)
{
Button button = base.AssociatedObject.Template.FindName("keyboardBtn", base.AssociatedObject) as Button;
if (button == null)
{
base.AssociatedObject.ApplyTemplate();
}
button = (base.AssociatedObject.Template.FindName("keyboardBtn", base.AssociatedObject) as Button);
if (button != null)
{
button.Click += new RoutedEventHandler(this.btn_Click);
button.Visibility = Visibility.Visible;
}
}
private void btn_Click(object sender, RoutedEventArgs e)
{
BindingExpression bindingExpression = BindingOperations.GetBindingExpression(base.AssociatedObject, TextBox.TextProperty);
Keyboard keyboard = new Keyboard();
Binding binding = new Binding();
binding.Source = bindingExpression.DataItem;
binding.Path = bindingExpression.ParentBinding.Path;
binding.Mode = bindingExpression.ParentBinding.Mode;
binding.UpdateSourceTrigger = bindingExpression.ParentBinding.UpdateSourceTrigger;
binding.NotifyOnTargetUpdated = bindingExpression.ParentBinding.NotifyOnTargetUpdated;
binding.NotifyOnSourceUpdated = bindingExpression.ParentBinding.NotifyOnSourceUpdated;
binding.NotifyOnValidationError = bindingExpression.ParentBinding.NotifyOnValidationError;
binding.ValidatesOnDataErrors = bindingExpression.ParentBinding.ValidatesOnDataErrors;
keyboard.TextTxt.SetBinding(TextBox.TextProperty, binding);
bool visibility = NavigationHelper.MainLayout.ModalBackground.Visibility != Visibility.Visible;
Util.ShowKeyboardForm(keyboard, visibility);
}
private object GetValue(string path, object dataItem)
{
string[] array = path.Split(new char[]
{
'.'
});
object obj = null;
System.Reflection.PropertyInfo property = dataItem.GetType().GetProperty(path);
object result;
if (property != null)
{
obj = property.GetValue(dataItem, null);
result = obj;
}
else
{
string[] array2 = array;
for (int i = 0; i < array2.Length; i++)
{
string name = array2[i];
if (obj == null)
{
property = dataItem.GetType().GetProperty(name);
obj = property.GetValue(dataItem, null);
}
else
{
property = obj.GetType().GetProperty(name);
obj = property.GetValue(obj, null);
}
}
result = obj;
}
return result;
}
}
}
error:
The name "TextBoxKeyboardBehavior" does not exist in the namespace "clr-namespace:MyProject.Fun.Core".
in backup of project it does work but in this case does not work !!!
all code are in same project and work last night but today does not work !

INotifyPropertyChanged and Threading

I have a base class implementing INotifyPropertyChanged:
protected void OnNotifyChanged(string pName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(pName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
I have a derived class with a property Latitude like so:
private double latitude;
public double Latitude
{
get { return latitude; }
set { latitude = value; OnNotifyChanged("Latitude"); }
}
My derived class also has a method Fly that manipulates Latitude.
I also have a Form with a TextBox bound to Latitude of my derived class:
txtLat.DataBindings.Clear();
txtLat.DataBindings.Add("Text", bindSrc, "Latitude");
A thread is used to kick off Fly like so:
Thread tFly = new Thread(f.Fly);
tFly.IsBackground = true;
tFly.Start();
When Latitude changes, an exception is thrown:
DataBinding cannot find a row in the list that is suitable for all bindings.
This seems to be an odd issue with thread affinity. Ultimately, the code is trying to do the update from a non-UI thread - I'm unclear why it isn't just displaying the cross-thread exception, though - I wonder whether this is actually a catch-all exception handler. If I remove the BindingSource (and bind directly to the object, which is valid) you do get a cross-thread exception (which I expected).
Personally, I would be inclined to handle this manually, i.e. subscribe to the event with a method that does an Invoke to the UI thread and updates the Text manually. However, I'm just checking if some previous cross-threaded binding code might help...
Here's an example using Invoke:
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
class FlightUav : INotifyPropertyChanged
{
protected void OnNotifyChanged(string pName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(pName));
}
public event PropertyChangedEventHandler PropertyChanged;
private double _latitude;
public double Latitude
{
get { return _latitude; }
set { _latitude = value; OnNotifyChanged("Latitude"); }
}
public void Fly()
{
for (int i = 0; i < 100; i++)
{
Latitude++;
Thread.Sleep(10);
}
}
[STAThread]
static void Main()
{
using (Form form = new Form())
{
FlightUav currentlyControlledFlightUav = new FlightUav();
currentlyControlledFlightUav.PropertyChanged += delegate
{ // this should be in a *regular* method so that you can -= it when changing bindings...
form.Invoke((MethodInvoker)delegate
{
form.Text = currentlyControlledFlightUav.Latitude.ToString();
});
};
using (Button btn = new Button())
{
btn.Text = "Fly";
btn.Click += delegate
{
Thread tFly = new Thread(currentlyControlledFlightUav.Fly);
tFly.IsBackground = true;
tFly.Start();
};
form.Controls.Add(btn);
Application.Run(form);
}
}
}
}
Here's an example using a (modified) version of some old threading code of mine:
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
class FlightUav : INotifyPropertyChanged
{
protected void OnNotifyChanged(string pName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(pName));
}
public event PropertyChangedEventHandler PropertyChanged;
private double _latitude;
public double Latitude
{
get { return _latitude; }
set { _latitude = value; OnNotifyChanged("Latitude"); }
}
public void Fly()
{
for (int i = 0; i < 100; i++)
{
Latitude++;
Thread.Sleep(10);
}
}
[STAThread]
static void Main()
{
using (Form form = new Form())
{
FlightUav currentlyControlledFlightUav = new FlightUav();
BindingSource bindSrc = new BindingSource();
var list = new ThreadedBindingList<FlightUav>();
list.Add(currentlyControlledFlightUav);
bindSrc.DataSource = list;
form.DataBindings.Clear();
form.DataBindings.Add("Text", list, "Latitude");
using (Button btn = new Button())
{
btn.Text = "Fly";
btn.Click += delegate
{
Thread tFly = new Thread(currentlyControlledFlightUav.Fly);
tFly.IsBackground = true;
tFly.Start();
};
form.Controls.Add(btn);
Application.Run(form);
}
}
}
}
public class ThreadedBindingList<T> : BindingList<T>
{
private readonly SynchronizationContext ctx;
public ThreadedBindingList()
{
ctx = SynchronizationContext.Current;
}
protected override void OnAddingNew(AddingNewEventArgs e)
{
SynchronizationContext ctx = SynchronizationContext.Current;
if (ctx == null)
{
BaseAddingNew(e);
}
else
{
ctx.Send(delegate
{
BaseAddingNew(e);
}, null);
}
}
void BaseAddingNew(AddingNewEventArgs e)
{
base.OnAddingNew(e);
}
protected override void OnListChanged(ListChangedEventArgs e)
{
if (ctx == null)
{
BaseListChanged(e);
}
else
{
ctx.Send(delegate
{
BaseListChanged(e);
}, null);
}
}
void BaseListChanged(ListChangedEventArgs e)
{
base.OnListChanged(e);
}
}

Can I dynamically make part of the TextBlock.Text to different colour?

I have TextBlock in my main form. I set the Text property to different strings during the application run.
I would like to be able to colour parts of particular strings.
Pseudo code:
if(a < 0) txbStatus.Text = string.Format("{0} <RED>{1}</RED>", a, b);
else txbStatus.Text = string.Format("{0} <BLUE>{1}</RED>", a, b);
You can split your string the way u want and then using a foreach() loop for that split string try
TextBlockName.Inlines.Add(new Run("colored text") {Foreground = Brushes.Blue});
The content of a TextBox doesn't have to be just a string, but a collection of Inlines:
txbStatus.Inlines.Clear();
txbStatus.Inlines.Add(new Run("normal color, "));
txbStatus.Inlines.Add(new Run("colored text") { Foreground = Brushes.Red });
I have created a custom TextBlock which will help you to highlight some part of text within the TextBlock's "Text" value.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace UI.WPF.UserControls
{
class CustomTextBlock:TextBlock
{
string _originalText;
public string HighlighText
{
get { return (string)GetValue(HighlighTextProperty); }
set
{
SetValue(HighlighTextProperty, value);
RenderHighlightedText();
}
}
// Using a DependencyProperty as the backing store for HighlighText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HighlighTextProperty =
DependencyProperty.Register("HighlighText", typeof(string), typeof(CustomTextBlock),
new FrameworkPropertyMetadata(new PropertyChangedCallback(HighlighTextProperty_Changed))
);
private static void HighlighTextProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CustomTextBlock textBlock = (CustomTextBlock)d;
textBlock.RenderHighlightedText();
}
public CustomTextBlock()
: base()
{
}
static CustomTextBlock()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(CustomTextBlock),
new FrameworkPropertyMetadata(typeof(CustomTextBlock)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
_originalText = Text;
RenderHighlightedText();
}
private Run GetFormatedText(string text, bool isBold)
{
Run noramlRun = new Run(text);
if (isBold)
{
noramlRun.FontWeight = FontWeights.Bold;
}
else
{
noramlRun.FontWeight = FontWeights.Normal;
}
return noramlRun;
}
public void RenderHighlightedText()
{
var boldText = HighlighText;
if (!string.IsNullOrEmpty(HighlighText) &&
_originalText.ToLower().Contains(boldText.ToLower()))
{
this.Inlines.Clear();
int point = _originalText.ToLower().IndexOf(boldText.ToLower());
string strHighlighted = _originalText.Substring(point, HighlighText.Length);
Run runHighlight = GetFormatedText(strHighlighted, true);
if (point == 0)
{
this.Inlines.Add(runHighlight);
int remainingLength = _originalText.Length - (point + HighlighText.Length);
string remaingText = _originalText.Substring((point + HighlighText.Length), remainingLength);
this.Inlines.Add(GetFormatedText(remaingText, false));
}
else
{
string firstPart = _originalText.Substring(0, point);
this.Inlines.Add(GetFormatedText(firstPart, false));
this.Inlines.Add(runHighlight);
int remainingLength = _originalText.Length - (point + HighlighText.Length);
string remaingText = _originalText.Substring((point + HighlighText.Length), remainingLength);
this.Inlines.Add(GetFormatedText(remaingText, false));
}
}
else
{
this.Inlines.Clear();
this.Inlines.Add(GetFormatedText(_originalText, false));
}
}
}
}
The Way to use it.
<usercontrol:CustomTextBlock Text="{Binding Title}"
HighlighText="{Binding DataContext.SearchText, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Page}}}"
/>
For more information
https://sites.google.com/site/greateindiaclub/mobil-apps/windows8/highlightpartoftextinwpftextblockcontrol

Categories