Related
I am trying for the image viewer in WPF using xamarin.forms, I did not find anything to start for the zoom functionalities. I am able to get only WPF or Android in xamarin. forms but unable to find the combination of WPF with xamarin.forms.
Please help me to achieve this.
EDIT
I have created the another view and able to zoom in and zoom out with the two buttons.
<ContentPage.Content>
<ScrollView>
<StackLayout>
<Image x:Name="ImageToDisplay"
HorizontalOptions="Center"
VerticalOptions="Center"
Aspect="AspectFill" >
</Image>
<StackLayout Orientation="Horizontal" Grid.Row="1" HorizontalOptions="CenterAndExpand">
<Button Text="ZoomOut" Clicked="ZoomOut"/>
<Button Text="ZoomIn" Clicked="ZoomIn"/>
</StackLayout>
</StackLayout>
</ScrollView>
</ContentPage.Content>
private void ZoomIn(object sender, EventArgs e)
{
var currentScale = ImageToDisplay.Scale;
if(currentScale>=1 && currentScale < 5)
{
ImageToDisplay.Scale = currentScale + 0.25;
}
}
private void ZoomOut(object sender, EventArgs e)
{
var currentScale = ImageToDisplay.Scale;
if (currentScale >= 1 && currentScale < 5)
{
ImageToDisplay.Scale = currentScale - 0.25;
}
}
But here, when the image is Zoom in, the remaining part of the image is not visible. the image in the scroll view is not scrolling.
You can custom Behaviors for Image to achieve this function.
Create a custom behavior class, MultiTouchBehavior.cs :
public class MultiTouchBehavior : Behavior<View>
{
#region Fields
private double _currentScale = 1, _startScale = 1, _xOffset, _yOffset;
private PinchGestureRecognizer _pinchGestureRecognizer;
private PanGestureRecognizer _panGestureRecognizer;
private ContentView _parent;
private View _associatedObject;
#endregion
/// <summary>
/// Occurs when BindingContext is changed: used to initialise the Gesture Recognizers.
/// </summary>
/// <param name="sender">The sender object.</param>
/// <param name="e">The event parameters.</param>
private void AssociatedObjectBindingContextChanged(object sender, EventArgs e)
{
_parent = _associatedObject.Parent as ContentView;
_parent?.GestureRecognizers.Remove(_panGestureRecognizer);
_parent?.GestureRecognizers.Add(_panGestureRecognizer);
_parent?.GestureRecognizers.Remove(_pinchGestureRecognizer);
_parent?.GestureRecognizers.Add(_pinchGestureRecognizer);
}
/// <summary>
/// Cleanup the events.
/// </summary>
private void CleanupEvents()
{
_pinchGestureRecognizer.PinchUpdated -= OnPinchUpdated;
_panGestureRecognizer.PanUpdated -= OnPanUpdated;
_associatedObject.BindingContextChanged -= AssociatedObjectBindingContextChanged;
}
/// <summary>
/// Initialise the events.
/// </summary>
private void InitializeEvents()
{
CleanupEvents();
_pinchGestureRecognizer.PinchUpdated += OnPinchUpdated;
_panGestureRecognizer.PanUpdated += OnPanUpdated;
_associatedObject.BindingContextChanged += AssociatedObjectBindingContextChanged;
}
/// <summary>
/// Initialise the Gesture Recognizers.
/// </summary>
private void InitialiseRecognizers()
{
_pinchGestureRecognizer = new PinchGestureRecognizer();
_panGestureRecognizer = new PanGestureRecognizer();
}
/// <summary>
/// Occurs when Behavior is attached to the View: initialises fields, properties and events.
/// </summary>
protected override void OnAttachedTo(View associatedObject)
{
InitialiseRecognizers();
_associatedObject = associatedObject;
InitializeEvents();
base.OnAttachedTo(associatedObject);
}
/// <summary>
/// Occurs when Behavior is detached from the View: cleanup fields, properties and events.
/// </summary>
protected override void OnDetachingFrom(View associatedObject)
{
CleanupEvents();
_parent = null;
_pinchGestureRecognizer = null;
_panGestureRecognizer = null;
_associatedObject = null;
base.OnDetachingFrom(associatedObject);
}
/// <summary>
/// Implements Pan/Translate.
/// </summary>
/// <param name="sender">The sender object.</param>
/// <param name="e">The event parameters.</param>
private void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
if (_parent == null)
{
return;
}
if (!IsTranslateEnabled)
{
return;
}
switch (e.StatusType)
{
case GestureStatus.Running:
_parent.Content.TranslationX = _xOffset + e.TotalX;
_parent.Content.TranslationY = _yOffset + e.TotalY;
break;
case GestureStatus.Completed:
_xOffset = _parent.Content.TranslationX;
_yOffset = _parent.Content.TranslationY;
break;
}
}
/// <summary>
/// Implements Pinch/Zoom.
/// </summary>
/// <param name="sender">The sender object.</param>
/// <param name="e">The event parameters.</param>
private void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
{
if (_parent == null)
{
return;
}
if (!IsScaleEnabled)
{
return;
}
switch (e.Status)
{
case GestureStatus.Started:
_startScale = _parent.Content.Scale;
_parent.Content.AnchorX = 0;
_parent.Content.AnchorY = 0;
break;
case GestureStatus.Running:
_currentScale += (e.Scale - 1) * _startScale;
_currentScale = Math.Max(1, _currentScale);
var renderedX = _parent.Content.X + _xOffset;
var deltaX = renderedX / _parent.Width;
var deltaWidth = _parent.Width / (_parent.Content.Width * _startScale);
var originX = (e.ScaleOrigin.X - deltaX) * deltaWidth;
var renderedY = _parent.Content.Y + _yOffset;
var deltaY = renderedY / _parent.Height;
var deltaHeight = _parent.Height / (_parent.Content.Height * _startScale);
var originY = (e.ScaleOrigin.Y - deltaY) * deltaHeight;
var targetX = _xOffset - (originX * _parent.Content.Width) * (_currentScale - _startScale);
var targetY = _yOffset - (originY * _parent.Content.Height) * (_currentScale - _startScale);
_parent.Content.TranslationX = targetX.Clamp(-_parent.Content.Width * (_currentScale - 1), 0);
_parent.Content.TranslationY = targetY.Clamp(-_parent.Content.Height * (_currentScale - 1), 0);
_parent.Content.Scale = _currentScale;
break;
case GestureStatus.Completed:
_xOffset = _parent.Content.TranslationX;
_yOffset = _parent.Content.TranslationY;
break;
}
}
/// <summary>
/// Initialize the behavior when OnAppearing is executed.
/// </summary>
public void OnAppearing()
{
AssociatedObjectBindingContextChanged(_associatedObject, null);
}
#region IsScaleEnabled property
/// <summary>
/// Identifies the <see cref="IsScaleEnabledProperty" /> property.
/// </summary>
public static readonly BindableProperty IsScaleEnabledProperty =
BindableProperty.Create<MultiTouchBehavior, bool>(w => w.IsScaleEnabled, default(bool));
/// <summary>
/// Identifies the <see cref="IsScaleEnabled" /> dependency / bindable property.
/// </summary>
public bool IsScaleEnabled
{
get { return (bool)GetValue(IsScaleEnabledProperty); }
set { SetValue(IsScaleEnabledProperty, value); }
}
#endregion
#region IsTranslateEnabled property
/// <summary>
/// Identifies the <see cref="IsTranslateEnabledProperty" /> property.
/// </summary>
public static readonly BindableProperty IsTranslateEnabledProperty =
BindableProperty.Create<MultiTouchBehavior, bool>(w => w.IsTranslateEnabled, default(bool));
/// <summary>
/// Identifies the <see cref="IsTranslateEnabled" /> dependency / bindable property.
/// </summary>
public bool IsTranslateEnabled
{
get { return (bool)GetValue(IsTranslateEnabledProperty); }
set { SetValue(IsTranslateEnabledProperty, value); }
}
#endregion
}
Add a DoubleExtensions.cs which used in MultiTouchBehavior.cs :
public static class DoubleExtensions
{
public static double Clamp(this double self, double min, double max)
{
return Math.Min(max, Math.Max(self, min));
}
}
Finally , in Xmal add two Button here:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:behaviors="clr-namespace:MultiTouch.Behaviors"
x:Class="MultiTouch.MainPage">
<StackLayout>
<ContentView >
<Image x:Name="MyImage" Source="desert.jpg">
<Image.Behaviors>
<behaviors:MultiTouchBehavior IsScaleEnabled="True" IsTranslateEnabled="True" />
</Image.Behaviors>
</Image>
</ContentView>
<Button Text="ZoomIn" Clicked="ZoomIn_Clicked"/>
<Button Text="ZoomOut" Clicked="ZoomOut_Clicked"/>
</StackLayout>
</ContentPage>
Button methods:
private void ZoomIn_Clicked(object sender, System.EventArgs e)
{
var currentScale = MyImage.Scale;
if (currentScale >= 1 && currentScale < 5)
{
MyImage.Scale = currentScale + 0.25;
}
}
private void ZoomOut_Clicked(object sender, System.EventArgs e)
{
var currentScale = MyImage.Scale;
if (currentScale >= 1 && currentScale < 5)
{
MyImage.Scale = currentScale - 0.25;
}
}
Here is the Sample link .
I am facing a few difficulties with EF 4, foreign keys and INotifyPropertyChanged / the partial methods exposed for scalar properties.
I hope you can help me find the right way to do this.
Image I have a Customer entity with *..1 relationship with the Country entity.
Now, I'd obviously like to be able to do:
var customer = new Customer();
customer.Country = [...]
...but I don't necessarily need the CountryKey property.
I create a Association in EF with the correct cardinality in the .edmx designer. I choose not to "add foreign key properties" in the dialog.
This leaves me with a generated class without the partial OnCountryChanging and OnCountryChanged.
Next, I try to add the foreign key properties, and I now have a OnCountryKeyChanging and OnCountryKeyChanged.
However, the generated code looks like this:
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int64 CountryKey
{
get
{
return _CountryKey;
}
set
{
OnCountryKeyChanging(value);
ReportPropertyChanging("CountryKey");
_CountryKey = StructuralObject.SetValidValue(value);
ReportPropertyChanged("CountryKey");
OnCountryKeyChanged();
}
}
private global::System.Int64 _CountryKey;
partial void OnCountryKeyChanging(global::System.Int64 value);
partial void OnCountryKeyChanged();
As you can see from the generated code, the PropertyChanged notification occurs with "CountryKey" instead of "Country". This makes data binding in WPF difficult.
My question is: how do I get around this?
Do I wrap my object in a ViewModel, listen to property changes and strip the "Key" part?
Do I modify the T4 template?
Or is there a third option I just can't see yet?
I'd greatly appreciate any suggestions here, as I am experimenting with WPF / EF without wrapping each Model property in a ViewModel.
The 'best practices' approach is to decorate your model in a viewmodel, exposing the model properties as required. You can create a generic ViewModel with some nifty work with dynamicobject, using perhaps a property mapping etc.
public class DynamicViewModel : DynamicObject, INotifyPropertyChanged, IDisposable
{
public event PropertyChangedEventHandler PropertyChanged;
private static readonly Dictionary<Type, Tuple<Dictionary<string, PropertyInfo>, Dictionary<string, string>>> typeDictionary = new Dictionary<Type, Tuple<Dictionary<string, PropertyInfo>, Dictionary<string, string>>>();
private readonly Dictionary<string, object> additionalProperties = new Dictionary<string, object>();
private readonly object underlyingObject;
public object UnderlyingObject
{
get
{
return underlyingObject;
}
}
private readonly Type type;
/// <summary>
/// constructor which takes a model for which it will be extensing its perceived properties
/// </summary>
/// <param name="underlyingObject">the underlying object</param>
public DynamicViewModel(IBindableRfqViewModel underlyingObject) : this(underlyingObject, new Dictionary<string, string>())
{
}
/// <summary>
/// constructor which takes a model for which it will be extensing its perceived properties as well as a property map
/// </summary>
/// <param name="underlyingObject">the underlying object</param>
/// <param name="propertyMap">a string/string dictionary, where the key is a property on the underlying object, and the value is the name of the dynamic property to be used as a binding target</param>
public DynamicViewModel(IBindableRfqViewModel underlyingObject, Dictionary<string, string> propertyMap)
{
this.underlyingObject = underlyingObject;
if (underlyingObject is INotifyPropertyChanged)
{
((INotifyPropertyChanged)underlyingObject).PropertyChanged += OnUnderlyingPropertyChanged;
}
type = underlyingObject.GetType();
if (typeDictionary.ContainsKey(type))
{
return;
}
lock (typeDictionary)
{
if (typeDictionary.ContainsKey(type))
{
return;
}
var forwardPropertyMap = propertyMap;
var typeProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToDictionary(p => p.Name, p => p);
typeDictionary.Add(type, Tuple.Create(typeProperties,forwardPropertyMap));
}
}
private void OnUnderlyingPropertyChanged(object sender, PropertyChangedEventArgs e)
{
this.OnPropertyChanged(e.PropertyName);
}
private bool TryGetProperty(string name, out object result)
{
try
{
var propertyData = typeDictionary[type];
var modelProperty = name;
if (propertyData.Item2.ContainsKey(name))
{
modelProperty = propertyData.Item2[name];
}
if (propertyData.Item1.ContainsKey(modelProperty))
{
result = propertyData.Item1[modelProperty].GetValue(underlyingObject, null);
return true;
}
if (additionalProperties.ContainsKey(name))
{
result = additionalProperties[name];
return true;
}
result = null;
return true;
}
catch (Exception ex)
{
result = null;
return false;
}
}
/// <summary>
/// <see cref="DynamicObject.TryGetMember" />
/// </summary>
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return this.TryGetProperty(binder.Name, out result);
}
/// <summary>
/// <see cref="DynamicObject.TryGetIndex" />
/// </summary>
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
{
return this.TryGetProperty(indexes[0].ToString(), out result);
}
private bool TrySetProperty(string name, object value)
{
try
{
var propertyData = typeDictionary[type];
var modelProperty = name;
if (propertyData.Item2.ContainsKey(name))
{
modelProperty = propertyData.Item2[name];
}
if (propertyData.Item1.ContainsKey(modelProperty))
{
propertyData.Item1[modelProperty].SetValue(underlyingObject, value, null);
}
else
{
if (!additionalProperties.ContainsKey(name))
{
additionalProperties.Add(name, new object());
}
additionalProperties[name] = value;
}
this.OnPropertyChanged(name);
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// <see cref="DynamicObject.TrySetMember" />
/// </summary>
public override bool TrySetMember(SetMemberBinder binder, object value)
{
return this.TrySetProperty(binder.Name, value);
}
/// <summary>
/// <see cref="DynamicObject.TrySetIndex" />
/// </summary>
public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
{
return indexes.Length == 0 || this.TrySetProperty(indexes[0].ToString(), value);
}
private void OnPropertyChanged(string propName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propName));
}
}
/// <summary>
/// IDisposable implementation
/// </summary>
public void Dispose()
{
if (underlyingObject is INotifyPropertyChanged)
{
((INotifyPropertyChanged)underlyingObject).PropertyChanged -= OnUnderlyingPropertyChanged;
}
if (underlyingObject is IDisposable)
{
((IDisposable)underlyingObject).Dispose();
}
}
}
We need to place a check box (and caption for it) in the header of a NavBarGroup. Is there a way to do this?
We created a NavBarGroupChecked class (NavBarGroupChecked.cs) that inherits from NavBarGroup and can just be dropped in to replace it. It adds a RepositoryItemCheckEdit member that tracks the checkbox and implements custom draw. It has a Checked property that tells you if it is checked and an event that will be called when the Checked status changes. That's pretty much it. Just drops in and works.
Code is below and also downloadable here.
// built from http://www.devexpress.com/example=E2061
using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraNavBar;
using DevExpress.XtraNavBar.ViewInfo;
namespace AutoTagCore.net.windward.controls
{
/// <summary>
/// A NavBarGroup that has a check box (with caption) in its header.
/// </summary>
public class NavBarGroupChecked : NavBarGroup
{
/// <summary>
/// Occurs when the Checked property value has been changed.
/// </summary>
public event EventHandler CheckedChanged;
private const int CHECK_BOX_WIDTH = 15;
private bool isLocked;
private RepositoryItemCheckEdit _GroupEdit;
private NavBarControl _NavBarControl;
private Rectangle hotRectangle;
/// <summary>
/// Initializes a new instance of the <see cref="T:DevExpress.XtraNavBar.NavBarGroup"/> class, with the specified caption.
/// </summary>
/// <param name="caption">A string representing the NavBar group's caption.</param>
public NavBarGroupChecked(string caption)
: base(caption)
{
ctor();
}
private void ctor()
{
GroupEdit = new RepositoryItemCheckEdit { GlyphAlignment = DevExpress.Utils.HorzAlignment.Far };
GroupEdit.Appearance.Options.UseTextOptions = true;
GroupEdit.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
GroupEdit.GlyphAlignment = DevExpress.Utils.HorzAlignment.Far;
ItemChanged += NavBarGroupChecked_ItemChanged;
}
private void NavBarGroupChecked_ItemChanged(object sender, System.EventArgs e)
{
if (NavBar != NavBarControl)
NavBarControl = NavBar;
}
/// <summary>
/// Creates an instance of the <see cref="T:DevExpress.XtraNavBar.NavBarGroup"/> class.
/// </summary>
public NavBarGroupChecked()
{
ctor();
}
/// <summary>
/// The NavBarControl that owns this. This must be set to work.
/// </summary>
private NavBarControl NavBarControl
{
get { return _NavBarControl; }
set { UnsubscribeEvents(value); _NavBarControl = value; SubscribeEvents(value); }
}
private void SubscribeEvents(NavBarControl navBarControl)
{
if (navBarControl == null)
return;
NavBarControl.CustomDrawGroupCaption += NavBarControl_CustomDrawGroupCaption;
NavBarControl.MouseClick += NavBarControl_MouseClick;
}
private void UnsubscribeEvents(NavBarControl navBarControl)
{
if (navBarControl != null)
return;
NavBarControl.CustomDrawGroupCaption -= NavBarControl_CustomDrawGroupCaption;
NavBarControl.MouseClick -= NavBarControl_MouseClick;
}
/// <summary>
/// true if the box is checked.
/// </summary>
public bool Checked { get; set; }
/// <summary>
/// The indent of the check box for the end of the header.
/// </summary>
public int CheckIndent { get; set; }
///<summary>
/// The check box displayed in the header.
///</summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public RepositoryItemCheckEdit GroupEdit
{
get { return _GroupEdit; }
set { _GroupEdit = value; }
}
private Rectangle GetCheckBoxBounds(Rectangle fixedCaptionBounds)
{
return new Rectangle(fixedCaptionBounds.Right - CHECK_BOX_WIDTH - CheckIndent, fixedCaptionBounds.Top, CHECK_BOX_WIDTH, fixedCaptionBounds.Height);
}
private bool IsCustomDrawNeeded(NavBarGroup group)
{
return GroupEdit != null && NavBarControl != null && !isLocked && group == this;
}
private void NavBarControl_CustomDrawGroupCaption(object sender, CustomDrawNavBarElementEventArgs e)
{
NavGroupInfoArgs infoArgs = (NavGroupInfoArgs) e.ObjectInfo;
if (!IsCustomDrawNeeded(infoArgs.Group))
return;
try
{
isLocked = true;
BaseNavGroupPainter painter = NavBarControl.View.CreateGroupPainter(NavBarControl);
Rectangle checkBoxBounds = GetCheckBoxBounds(infoArgs.CaptionBounds);
painter.DrawObject(infoArgs);
DrawCheckBox(e.Graphics, checkBoxBounds);
e.Handled = true;
}
finally
{
isLocked = false;
}
}
private void DrawCheckBox(Graphics g, Rectangle r)
{
BaseEditPainter painter = GroupEdit.CreatePainter();
BaseEditViewInfo info = GroupEdit.CreateViewInfo();
info.EditValue = Checked;
SizeF textBounds = info.Appearance.CalcTextSize(g, GroupEdit.Caption, 500);
int totalWidth = (int)textBounds.Width + r.Width + 10;
info.Bounds = new Rectangle(r.Right - totalWidth, r.Y, totalWidth, r.Height);
info.CalcViewInfo(g);
ControlGraphicsInfoArgs args = new ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
painter.Draw(args);
args.Cache.Dispose();
}
private static NavBarViewInfo GetNavBarView(NavBarControl NavBar)
{
PropertyInfo pi = typeof(NavBarControl).GetProperty("ViewInfo", BindingFlags.Instance | BindingFlags.NonPublic);
return pi.GetValue(NavBar, null) as NavBarViewInfo;
}
private bool IsCheckBox(Point p)
{
NavBarHitInfo hi = NavBarControl.CalcHitInfo(p);
if (hi.Group == null || hi.Group != this)
return false;
NavBarViewInfo vi = GetNavBarView(NavBarControl);
vi.Calc(NavBarControl.ClientRectangle);
NavGroupInfoArgs groupInfo = vi.GetGroupInfo(hi.Group);
Rectangle checkBounds = GetCheckBoxBounds(groupInfo.CaptionBounds);
hotRectangle = checkBounds;
return checkBounds.Contains(p);
}
private void NavBarControl_MouseClick(object sender, MouseEventArgs e)
{
if (!IsCheckBox(e.Location))
return;
Checked = !Checked;
NavBarControl.Invalidate(hotRectangle);
if (CheckedChanged != null)
CheckedChanged(sender, e);
}
}
}
I had some issues with the way that solution worked and made some minor tweaks. See below.
A sample on how to use this is;
private void Form1_Load(object sender, EventArgs e)
{
var item = navBarControl1.Groups.Add(new NavBarGroupChecked("NavBarGroupCheckbox", navBarControl1)) as NavBarGroupChecked;
item.Hint = "my hint";
item.CheckedChanged += checkchanged;
}
private void checkchanged(object sender, EventArgs e)
{
MessageBox.Show("It Changed");
}
This is the solution:
// built from http://www.devexpress.com/example=E2061
using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraNavBar;
using DevExpress.XtraNavBar.ViewInfo;
namespace NavBarCheckTest
{
/// <summary>
/// A NavBarGroup that has a check box (with caption) in its header.
/// </summary>
public class NavBarGroupChecked : NavBarGroup
{
/// <summary>
/// Occurs when the Checked property value has been changed.
/// </summary>
public event EventHandler CheckedChanged;
private const int CHECK_BOX_WIDTH = 15;
private bool isLocked;
private RepositoryItemCheckEdit _GroupEdit;
private NavBarControl _NavBarControl;
private Rectangle hotRectangle;
/// <summary>
/// Initializes a new instance of the <see cref="T:DevExpress.XtraNavBar.NavBarGroup"/> class, with the specified caption.
/// </summary>
/// <param name="caption">A string representing the NavBar group's caption.</param>
public NavBarGroupChecked(string caption, NavBarControl parent = null)
: base(caption)
{
ctor(parent);
}
private void ctor(NavBarControl parent = null)
{
GroupEdit = new RepositoryItemCheckEdit { GlyphAlignment = DevExpress.Utils.HorzAlignment.Far };
GroupEdit.Caption = string.Empty;
GroupEdit.Appearance.Options.UseTextOptions = true;
GroupEdit.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
GroupEdit.GlyphAlignment = DevExpress.Utils.HorzAlignment.Far;
ItemChanged += NavBarGroupChecked_ItemChanged;
if (parent != null)
NavBarControl = parent;
}
private void NavBarGroupChecked_ItemChanged(object sender, System.EventArgs e)
{
if (NavBar != NavBarControl)
NavBarControl = NavBar;
}
/// <summary>
/// Creates an instance of the <see cref="T:DevExpress.XtraNavBar.NavBarGroup"/> class.
/// </summary>
public NavBarGroupChecked(NavBarControl parent = null)
{
ctor(parent);
}
/// <summary>
/// The NavBarControl that owns this. This must be set to work.
/// </summary>
private NavBarControl NavBarControl
{
get { return _NavBarControl; }
set { UnsubscribeEvents(value); _NavBarControl = value; SubscribeEvents(value); }
}
private void SubscribeEvents(NavBarControl navBarControl)
{
if (navBarControl == null)
return;
NavBarControl.CustomDrawGroupCaption += NavBarControl_CustomDrawGroupCaption;
NavBarControl.MouseClick += NavBarControl_MouseClick;
}
private void UnsubscribeEvents(NavBarControl navBarControl)
{
if (navBarControl != null)
return;
NavBarControl.CustomDrawGroupCaption -= NavBarControl_CustomDrawGroupCaption;
NavBarControl.MouseClick -= NavBarControl_MouseClick;
}
/// <summary>
/// true if the box is checked.
/// </summary>
public bool Checked { get; set; }
/// <summary>
/// The indent of the check box for the end of the header.
/// </summary>
public int CheckIndent { get; set; }
///<summary>
/// The check box displayed in the header.
///</summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public RepositoryItemCheckEdit GroupEdit
{
get { return _GroupEdit; }
set { _GroupEdit = value; }
}
private int GetCheckBoxWidth()
{
return CheckIndent * 2 + 10;
}
//private Rectangle GetCaptionBounds(Rectangle originalCaptionBounds)
//{
// return new Rectangle(originalCaptionBounds.Location, new Size(originalCaptionBounds.Width - GetCheckBoxWidth(), originalCaptionBounds.Height));
//}
private Rectangle GetCheckBoxBounds(Rectangle fixedCaptionBounds)
{
return new Rectangle(fixedCaptionBounds.Right - CHECK_BOX_WIDTH - CheckIndent, fixedCaptionBounds.Top, CHECK_BOX_WIDTH, fixedCaptionBounds.Height);
//return new Rectangle(fixedCaptionBounds.Right, fixedCaptionBounds.Top, GetCheckBoxWidth(), fixedCaptionBounds.Height); ;
}
private bool IsCustomDrawNeeded(NavBarGroup group)
{
return GroupEdit != null && NavBarControl != null && !isLocked && group == this;
}
private void NavBarControl_CustomDrawGroupCaption(object sender, CustomDrawNavBarElementEventArgs e)
{
NavGroupInfoArgs infoArgs = (NavGroupInfoArgs) e.ObjectInfo;
if (!IsCustomDrawNeeded(infoArgs.Group))
return;
try
{
isLocked = true;
BaseNavGroupPainter painter = NavBarControl.View.CreateGroupPainter(NavBarControl);
Rectangle originalCaptionBounds = new Rectangle(infoArgs.CaptionClientBounds.X, infoArgs.CaptionClientBounds.Y, infoArgs.CaptionClientBounds.Width - infoArgs.ButtonBounds.Width, infoArgs.CaptionClientBounds.Height);
Rectangle checkBoxBounds = GetCheckBoxBounds(originalCaptionBounds);
painter.DrawObject(infoArgs);
DrawCheckBox(e.Graphics, checkBoxBounds);
e.Handled = true;
}
finally
{
isLocked = false;
}
}
private void DrawCheckBox(Graphics g, Rectangle r)
{
BaseEditPainter painter = GroupEdit.CreatePainter();
BaseEditViewInfo info = GroupEdit.CreateViewInfo();
info.EditValue = Checked;
SizeF textBounds = info.Appearance.CalcTextSize(g, GroupEdit.Caption, 500);
int totalWidth = (int)textBounds.Width + r.Width + 10;
info.Bounds = new Rectangle(r.Right - totalWidth, r.Y, totalWidth, r.Height);
info.CalcViewInfo(g);
ControlGraphicsInfoArgs args = new ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
painter.Draw(args);
args.Cache.Dispose();
}
private static NavBarViewInfo GetNavBarView(NavBarControl NavBar)
{
PropertyInfo pi = typeof(NavBarControl).GetProperty("ViewInfo", BindingFlags.Instance | BindingFlags.NonPublic);
return pi.GetValue(NavBar, null) as NavBarViewInfo;
}
private bool IsCheckBox(Point p)
{
NavBarHitInfo hi = NavBarControl.CalcHitInfo(p);
if (hi.Group == null || hi.Group != this)
return false;
NavBarViewInfo vi = GetNavBarView(NavBarControl);
vi.Calc(NavBarControl.ClientRectangle);
NavGroupInfoArgs groupInfo = vi.GetGroupInfo(hi.Group);
Rectangle originalCaptionBounds = new Rectangle(groupInfo.CaptionClientBounds.X, groupInfo.CaptionClientBounds.Y, groupInfo.CaptionClientBounds.Width - groupInfo.ButtonBounds.Width, groupInfo.CaptionClientBounds.Height);
Rectangle checkBounds = GetCheckBoxBounds(originalCaptionBounds);
hotRectangle = checkBounds;
return checkBounds.Contains(p);
}
private void NavBarControl_MouseClick(object sender, MouseEventArgs e)
{
if (!IsCheckBox(e.Location))
return;
Checked = !Checked;
NavBarControl.Invalidate(hotRectangle);
if (CheckedChanged != null)
CheckedChanged(sender, e);
}
}
}
I need to be able to specify a command to run when the SelectionChanged event fires. I already know how to implement the ICommandSource interface; what I need to know is how I can just add a command to the column series to handle the SelectionChanged event.
When I inherit from the ColumnBarBaseSeries<...> base class I have to override GetAxes() and UpdateDatePoint() which I am not sure how to implement.
You can use attached behaviours to solve this problem.
Create a SelectionChangedBehaviour that wires a selectionChanged event to the element that you're attaching the behaviour to then you can binding any ICommand to that behaviour.
For more on attached behaviours -
Introduction article by Josh Smith
Overview of the concept
Another good introduction
Hope that helps
Here is some code to add an attached behavior to a ColumnSeries for a SelectionChanged Command.
public static class ColumnSeriesBehavior
{
private static DelegateCommand<object> SelectionChangedCommand;
public static DelegateCommand<object> GetSelectionChangedCommand(ColumnSeries cs)
{
return cs.GetValue(SelectionChangedCommandProperty) as DelegateCommand<object>;
}
public static void SetSelectionChangedCommand(ColumnSeries cs, DelegateCommand<object> value)
{
cs.SetValue(SelectionChangedCommandProperty, value);
}
// Using a DependencyProperty as the backing store for SelectionChangedCommand. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectionChangedCommandProperty =
DependencyProperty.RegisterAttached("SelectionChangedCommand", typeof(DelegateCommand<object>), typeof(ColumnSeriesBehavior), new UIPropertyMetadata(null, OnSelectionChangedCommandChanged));
private static void OnSelectionChangedCommandChanged(
DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
ColumnSeries item = depObj as ColumnSeries;
if (item == null)
{
return;
}
if (e.NewValue is DelegateCommand<object> == false)
{
return;
}
SelectionChangedCommand = e.NewValue as DelegateCommand<object>;
item.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(Column_SelectionChanged);
}
private static void Column_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (SelectionChangedCommand != null)
SelectionChangedCommand.Execute(sender);
}
}
And in the XAML to attach the property:
<chartingToolkit:Chart.Series>
<chartingToolkit:ColumnSeries
IsSelectionEnabled="True"
ItemsSource="{Binding YourItemSource}"
IndependentValueBinding="{Binding YourIndValue, Path=YourIndValuePath}"
DependentValueBinding="{Binding YourDepValue, Path=YourDepValuePath}"
>
<chartingToolkit:ColumnSeries.Style>
<Style>
<!-- Attaching the SelectionChangedCommand behavior -->
<Setter Property="local:ColumnSeriesBehavior.SelectionChangedCommand"
Value="{Binding YourDelegateCommand}"/>
</Style>
</chartingToolkit:ColumnSeries.Style>
</chartingToolkit:ColumnSeries>
</chartingToolkit:Chart.Series>
Here is some code that appears to work for me to implement your own CommandColumnSeries, I stole a lot of it from the source for the ColumnSeries Sealed Class:
public class CommandColumnSeries : ColumnBarBaseSeries<ColumnDataPoint>
{
#region "ICommandSource"
[Localizability(LocalizationCategory.NeverLocalize), Category("Action"), Bindable(true)]
public ICommand Command
{
get
{
return (ICommand)base.GetValue(CommandProperty);
}
set
{
base.SetValue(CommandProperty, value);
}
}
[Bindable(true), Category("Action"), Localizability(LocalizationCategory.NeverLocalize)]
public object CommandParameter
{
get
{
return base.GetValue(CommandParameterProperty);
}
set
{
base.SetValue(CommandParameterProperty, value);
}
}
[Category("Action"), Bindable(true)]
public IInputElement CommandTarget
{
get
{
return (IInputElement)base.GetValue(CommandTargetProperty);
}
set
{
base.SetValue(CommandTargetProperty, value);
}
}
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(CommandColumnSeries), new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandColumnSeries), new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(CommandColumnSeries), new FrameworkPropertyMetadata(null));
#endregion
#region public IRangeAxis DependentRangeAxis
/// <summary>
/// Gets or sets the dependent range axis.
/// </summary>
public IRangeAxis DependentRangeAxis
{
get { return GetValue(DependentRangeAxisProperty) as IRangeAxis; }
set { SetValue(DependentRangeAxisProperty, value); }
}
/// <summary>
/// Identifies the DependentRangeAxis dependency property.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes", Justification = "This member is necessary because the base classes need to share this dependency property.")]
public static readonly DependencyProperty DependentRangeAxisProperty =
DependencyProperty.Register(
"DependentRangeAxis",
typeof(IRangeAxis),
typeof(ColumnSeries),
new PropertyMetadata(null, OnDependentRangeAxisPropertyChanged));
/// <summary>
/// DependentRangeAxisProperty property changed handler.
/// </summary>
/// <param name="d">ColumnBarBaseSeries that changed its DependentRangeAxis.</param>
/// <param name="e">Event arguments.</param>
private static void OnDependentRangeAxisPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CommandColumnSeries source = (CommandColumnSeries)d;
IRangeAxis newValue = (IRangeAxis)e.NewValue;
source.OnDependentRangeAxisPropertyChanged(newValue);
}
/// <summary>
/// DependentRangeAxisProperty property changed handler.
/// </summary>
/// <param name="newValue">New value.</param>
private void OnDependentRangeAxisPropertyChanged(IRangeAxis newValue)
{
this.InternalDependentAxis = (IAxis)newValue;
}
#endregion public IRangeAxis DependentRangeAxis
#region public IAxis IndependentAxis
/// <summary>
/// Gets or sets the independent category axis.
/// </summary>
public IAxis IndependentAxis
{
get { return GetValue(IndependentAxisProperty) as IAxis; }
set { SetValue(IndependentAxisProperty, value); }
}
/// <summary>
/// Identifies the IndependentAxis dependency property.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes", Justification = "This member is necessary because the base classes need to share this dependency property.")]
public static readonly DependencyProperty IndependentAxisProperty =
DependencyProperty.Register(
"IndependentAxis",
typeof(IAxis),
typeof(ColumnSeries),
new PropertyMetadata(null, OnIndependentAxisPropertyChanged));
/// <summary>
/// IndependentAxisProperty property changed handler.
/// </summary>
/// <param name="d">ColumnBarBaseSeries that changed its IndependentAxis.</param>
/// <param name="e">Event arguments.</param>
private static void OnIndependentAxisPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CommandColumnSeries source = (CommandColumnSeries)d;
IAxis newValue = (IAxis)e.NewValue;
source.OnIndependentAxisPropertyChanged(newValue);
}
/// <summary>
/// IndependentAxisProperty property changed handler.
/// </summary>
/// <param name="newValue">New value.</param>
private void OnIndependentAxisPropertyChanged(IAxis newValue)
{
this.InternalIndependentAxis = (IAxis)newValue;
}
#endregion public IAxis IndependentAxis
public CommandColumnSeries()
{
this.SelectionChanged += new SelectionChangedEventHandler(CommandColumnSeries_SelectionChanged);
}
private void CommandColumnSeries_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (Command != null)
{
RoutedCommand routedCommand = Command as RoutedCommand;
CommandParameter = e.Source;
if (routedCommand != null)
{
routedCommand.Execute(CommandParameter, CommandTarget);
}
else
{
Command.Execute(CommandParameter);
}
}
}
protected override void GetAxes(DataPoint firstDataPoint)
{
// Taken from the source of the ColumnSeries sealed class.
GetAxes(
firstDataPoint,
(axis) => axis.Orientation == AxisOrientation.X,
() => new CategoryAxis { Orientation = AxisOrientation.X },
(axis) =>
{
IRangeAxis rangeAxis = axis as IRangeAxis;
return rangeAxis != null && rangeAxis.Origin != null && axis.Orientation == AxisOrientation.Y;
},
() =>
{
IRangeAxis rangeAxis = CreateRangeAxisFromData(firstDataPoint.DependentValue);
rangeAxis.Orientation = AxisOrientation.Y;
if (rangeAxis == null || rangeAxis.Origin == null)
{
throw new InvalidOperationException("No Suitable Axes found for plotting range axis.");
}
DisplayAxis axis = rangeAxis as DisplayAxis;
if (axis != null)
{
axis.ShowGridLines = true;
}
return rangeAxis;
});
}
protected override void UpdateDataPoint(DataPoint dataPoint)
{
// This code taken from the ColumnSeries sealed class.
if (SeriesHost == null )//|| PlotArea == null)
{
return;
}
object category = dataPoint.ActualIndependentValue ?? (IndexOf<DataPoint>(this.ActiveDataPoints, dataPoint) + 1);
Range<UnitValue> coordinateRange = GetCategoryRange(category);
if (!coordinateRange.HasData)
{
return;
}
else if (coordinateRange.Maximum.Unit != Unit.Pixels || coordinateRange.Minimum.Unit != Unit.Pixels)
{
throw new InvalidOperationException("This Series Does Not Support Radial Axes");
}
double minimum = (double)coordinateRange.Minimum.Value;
double maximum = (double)coordinateRange.Maximum.Value;
double plotAreaHeight = ActualDependentRangeAxis.GetPlotAreaCoordinate(ActualDependentRangeAxis.Range.Maximum).Value.Value;
IEnumerable<CommandColumnSeries> columnSeries = SeriesHost.Series.OfType<CommandColumnSeries>().Where(series => series.ActualIndependentAxis == ActualIndependentAxis);
int numberOfSeries = columnSeries.Count();
double coordinateRangeWidth = (maximum - minimum);
double segmentWidth = coordinateRangeWidth * 0.8;
double columnWidth = segmentWidth / numberOfSeries;
int seriesIndex = IndexOf<CommandColumnSeries>(columnSeries, this);
double dataPointY = ActualDependentRangeAxis.GetPlotAreaCoordinate(ToDouble(dataPoint.ActualDependentValue)).Value.Value;
double zeroPointY = ActualDependentRangeAxis.GetPlotAreaCoordinate(ActualDependentRangeAxis.Origin).Value.Value;
double offset = seriesIndex * Math.Round(columnWidth) + coordinateRangeWidth * 0.1;
double dataPointX = minimum + offset;
if (GetIsDataPointGrouped(category))
{
// Multiple DataPoints share this category; offset and overlap them appropriately
IGrouping<object, DataPoint> categoryGrouping = GetDataPointGroup(category);
int index = GroupIndexOf(categoryGrouping, dataPoint);
dataPointX += (index * (columnWidth * 0.2)) / (categoryGrouping.Count() - 1);
columnWidth *= 0.8;
Canvas.SetZIndex(dataPoint, -index);
}
if (CanGraph(dataPointY) && CanGraph(dataPointX) && CanGraph(zeroPointY))
{
double left = Math.Round(dataPointX);
double width = Math.Round(columnWidth);
double top = Math.Round(plotAreaHeight - Math.Max(dataPointY, zeroPointY) + 0.5);
double bottom = Math.Round(plotAreaHeight - Math.Min(dataPointY, zeroPointY) + 0.5);
double height = bottom - top + 1;
Canvas.SetLeft(dataPoint, left);
Canvas.SetTop(dataPoint, top);
dataPoint.Width = width;
dataPoint.Height = height;
}
}
private static int IndexOf<T>(IEnumerable<T> collection, T target)
{
int i = 0;
foreach (var obj in collection)
{
if (obj.Equals(target))
return i;
i++;
}
return -1;
}
private static int GroupIndexOf(IGrouping<object, DataPoint> group, DataPoint point)
{
int i = 0;
foreach (var pt in group)
{
if (pt == point)
return i;
i++;
}
return -1;
}
/// <summary>
/// Returns a value indicating whether this value can be graphed on a
/// linear axis.
/// </summary>
/// <param name="value">The value to evaluate.</param>
/// <returns>A value indicating whether this value can be graphed on a
/// linear axis.</returns>
private static bool CanGraph(double value)
{
return !double.IsNaN(value) && !double.IsNegativeInfinity(value) && !double.IsPositiveInfinity(value) && !double.IsInfinity(value);
}
/// <summary>
/// Converts an object into a double.
/// </summary>
/// <param name="value">The value to convert to a double.</param>
/// <returns>The converted double value.</returns>
private static double ToDouble(object value)
{
return Convert.ToDouble(value, CultureInfo.InvariantCulture);
}
}
Short Version
Calls to CommandManager.InvalidateRequerySuggested() take far longer to take effect than I would like (1-2 second delay before UI controls become disabled).
Long Version
I have a system where I submit tasks to a background-thread based task processor. This submit happens on the WPF UI thread.
When this submit happens, the object that manages my background thread does two things:
It raises a "busy" event (still on the UI thread) that several view models respond to; when they receive this event, they set an IsEnabled flag on themselves to false. Controls in my views, which are databound to this property, are immediately grayed out, which is what I would expect.
It informs my WPF ICommand objects that they should not be allowed to execute (again, still on the UI thread). Because there is nothing like INotifyPropertyChanged for ICommand objects, I am forced to call CommandManager.InvalidateRequerySuggested() to force WPF to reconsider all of my command objects' CanExecute states (yes, I actually do need to do this: otherwise, none of these controls become disabled). Unlike item 1, though, it takes a significantly longer time for my buttons/menu items/etc that are using ICommand objects to visually change to a disabled state than it does for the UI controls that have their IsEnabled property manually set.
The problem is, from a UX point of view, this looks awful; half of my controls are immediately grayed out (because their IsEnabled property is set to false), and then a full 1-2 seconds later, the other half of my controls follow suit (because their CanExecute methods are finally re-evaluated).
So, part 1 of my question:
As silly as it sounds to ask, is there a way I can make CommandManager.InvalidateRequerySuggested() do it's job faster? I suspect that there isn't.
Fair enough, part 2 of my question:
How can I work around this? I'd prefer all of my controls be disabled at the same time. It just looks unprofessional and awkward otherwise. Any ideas? :-)
CommandManager.InvalidateRequerySuggested() tries to validate all commands, which is totally ineffective (and in your case slow) - on every change, you are asking every command to recheck its CanExecute()!
You'd need the command to know on which objects and properties is its CanExecute dependent, and suggest requery only when they change. That way, if you change a property of an object, only commands that depend on it will change their state.
This is how I solved the problem, but at first, a teaser:
// in ViewModel's constructor - add a code to public ICommand:
this.DoStuffWithParameterCommand = new DelegateCommand<object>(
parameter =>
{
//do work with parameter (remember to check against null)
},
parameter =>
{
//can this command execute? return true or false
}
)
.ListenOn(whichObject, n => n.ObjectProperty /*type safe!*/, this.Dispatcher /*we need to pass UI dispatcher here*/)
.ListenOn(anotherObject, n => n.AnotherObjectProperty, this.Dispatcher); // chain calling!
The command is listening on NotifyPropertyChanged events from object that affect whether it can execute, and invokes the check only when a requery is needed.
Now, a lot of code (part of our in-house framework) to do this:
I use DelegateCommand from Prism, that looks like this:
/// <summary>
/// This class allows delegating the commanding logic to methods passed as parameters,
/// and enables a View to bind commands to objects that are not part of the element tree.
/// </summary>
public class DelegateCommand : ICommand
{
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public DelegateCommand(Action executeMethod)
: this(executeMethod, null, false)
{
}
/// <summary>
/// Constructor
/// </summary>
public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod)
: this(executeMethod, canExecuteMethod, false)
{
}
/// <summary>
/// Constructor
/// </summary>
public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod, bool isAutomaticRequeryDisabled)
{
if (executeMethod == null)
{
throw new ArgumentNullException("executeMethod");
}
_executeMethod = executeMethod;
_canExecuteMethod = canExecuteMethod;
_isAutomaticRequeryDisabled = isAutomaticRequeryDisabled;
this.RaiseCanExecuteChanged();
}
#endregion
#region Public Methods
/// <summary>
/// Method to determine if the command can be executed
/// </summary>
public bool CanExecute()
{
if (_canExecuteMethod != null)
{
return _canExecuteMethod();
}
return true;
}
/// <summary>
/// Execution of the command
/// </summary>
public void Execute()
{
if (_executeMethod != null)
{
_executeMethod();
}
}
/// <summary>
/// Property to enable or disable CommandManager's automatic requery on this command
/// </summary>
public bool IsAutomaticRequeryDisabled
{
get
{
return _isAutomaticRequeryDisabled;
}
set
{
if (_isAutomaticRequeryDisabled != value)
{
if (value)
{
CommandManagerHelper.RemoveHandlersFromRequerySuggested(_canExecuteChangedHandlers);
}
else
{
CommandManagerHelper.AddHandlersToRequerySuggested(_canExecuteChangedHandlers);
}
_isAutomaticRequeryDisabled = value;
}
}
}
/// <summary>
/// Raises the CanExecuteChaged event
/// </summary>
public void RaiseCanExecuteChanged()
{
OnCanExecuteChanged();
}
/// <summary>
/// Protected virtual method to raise CanExecuteChanged event
/// </summary>
protected virtual void OnCanExecuteChanged()
{
CommandManagerHelper.CallWeakReferenceHandlers(_canExecuteChangedHandlers);
}
#endregion
#region ICommand Members
/// <summary>
/// ICommand.CanExecuteChanged implementation
/// </summary>
public event EventHandler CanExecuteChanged
{
add
{
if (!_isAutomaticRequeryDisabled)
{
CommandManager.RequerySuggested += value;
}
CommandManagerHelper.AddWeakReferenceHandler(ref _canExecuteChangedHandlers, value, 2);
}
remove
{
if (!_isAutomaticRequeryDisabled)
{
CommandManager.RequerySuggested -= value;
}
CommandManagerHelper.RemoveWeakReferenceHandler(_canExecuteChangedHandlers, value);
}
}
bool ICommand.CanExecute(object parameter)
{
return CanExecute();
}
void ICommand.Execute(object parameter)
{
Execute();
}
#endregion
#region Data
private readonly Action _executeMethod = null;
private readonly Func<bool> _canExecuteMethod = null;
private bool _isAutomaticRequeryDisabled = false;
private List<WeakReference> _canExecuteChangedHandlers;
#endregion
}
/// <summary>
/// This class allows delegating the commanding logic to methods passed as parameters,
/// and enables a View to bind commands to objects that are not part of the element tree.
/// </summary>
/// <typeparam name="T">Type of the parameter passed to the delegates</typeparam>
public class DelegateCommand<T> : ICommand
{
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public DelegateCommand(Action<T> executeMethod)
: this(executeMethod, null, false)
{
}
/// <summary>
/// Constructor
/// </summary>
public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
: this(executeMethod, canExecuteMethod, false)
{
}
/// <summary>
/// Constructor
/// </summary>
public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod, bool isAutomaticRequeryDisabled)
{
if (executeMethod == null)
{
throw new ArgumentNullException("executeMethod");
}
_executeMethod = executeMethod;
_canExecuteMethod = canExecuteMethod;
_isAutomaticRequeryDisabled = isAutomaticRequeryDisabled;
}
#endregion
#region Public Methods
/// <summary>
/// Method to determine if the command can be executed
/// </summary>
public bool CanExecute(T parameter)
{
if (_canExecuteMethod != null)
{
return _canExecuteMethod(parameter);
}
return true;
}
/// <summary>
/// Execution of the command
/// </summary>
public void Execute(T parameter)
{
if (_executeMethod != null)
{
_executeMethod(parameter);
}
}
/// <summary>
/// Raises the CanExecuteChaged event
/// </summary>
public void RaiseCanExecuteChanged()
{
OnCanExecuteChanged();
}
/// <summary>
/// Protected virtual method to raise CanExecuteChanged event
/// </summary>
protected virtual void OnCanExecuteChanged()
{
CommandManagerHelper.CallWeakReferenceHandlers(_canExecuteChangedHandlers);
}
/// <summary>
/// Property to enable or disable CommandManager's automatic requery on this command
/// </summary>
public bool IsAutomaticRequeryDisabled
{
get
{
return _isAutomaticRequeryDisabled;
}
set
{
if (_isAutomaticRequeryDisabled != value)
{
if (value)
{
CommandManagerHelper.RemoveHandlersFromRequerySuggested(_canExecuteChangedHandlers);
}
else
{
CommandManagerHelper.AddHandlersToRequerySuggested(_canExecuteChangedHandlers);
}
_isAutomaticRequeryDisabled = value;
}
}
}
#endregion
#region ICommand Members
/// <summary>
/// ICommand.CanExecuteChanged implementation
/// </summary>
public event EventHandler CanExecuteChanged
{
add
{
if (!_isAutomaticRequeryDisabled)
{
CommandManager.RequerySuggested += value;
}
CommandManagerHelper.AddWeakReferenceHandler(ref _canExecuteChangedHandlers, value, 2);
}
remove
{
if (!_isAutomaticRequeryDisabled)
{
CommandManager.RequerySuggested -= value;
}
CommandManagerHelper.RemoveWeakReferenceHandler(_canExecuteChangedHandlers, value);
}
}
bool ICommand.CanExecute(object parameter)
{
// if T is of value type and the parameter is not
// set yet, then return false if CanExecute delegate
// exists, else return true
if (parameter == null &&
typeof(T).IsValueType)
{
return (_canExecuteMethod == null);
}
return CanExecute((T)parameter);
}
void ICommand.Execute(object parameter)
{
Execute((T)parameter);
}
#endregion
#region Data
private readonly Action<T> _executeMethod = null;
private readonly Func<T, bool> _canExecuteMethod = null;
private bool _isAutomaticRequeryDisabled = false;
private List<WeakReference> _canExecuteChangedHandlers;
#endregion
}
/// <summary>
/// This class contains methods for the CommandManager that help avoid memory leaks by
/// using weak references.
/// </summary>
internal class CommandManagerHelper
{
internal static void CallWeakReferenceHandlers(List<WeakReference> handlers)
{
if (handlers != null)
{
// Take a snapshot of the handlers before we call out to them since the handlers
// could cause the array to me modified while we are reading it.
EventHandler[] callees = new EventHandler[handlers.Count];
int count = 0;
for (int i = handlers.Count - 1; i >= 0; i--)
{
WeakReference reference = handlers[i];
EventHandler handler = reference.Target as EventHandler;
if (handler == null)
{
// Clean up old handlers that have been collected
handlers.RemoveAt(i);
}
else
{
callees[count] = handler;
count++;
}
}
// Call the handlers that we snapshotted
for (int i = 0; i < count; i++)
{
EventHandler handler = callees[i];
handler(null, EventArgs.Empty);
}
}
}
internal static void AddHandlersToRequerySuggested(List<WeakReference> handlers)
{
if (handlers != null)
{
foreach (WeakReference handlerRef in handlers)
{
EventHandler handler = handlerRef.Target as EventHandler;
if (handler != null)
{
CommandManager.RequerySuggested += handler;
}
}
}
}
internal static void RemoveHandlersFromRequerySuggested(List<WeakReference> handlers)
{
if (handlers != null)
{
foreach (WeakReference handlerRef in handlers)
{
EventHandler handler = handlerRef.Target as EventHandler;
if (handler != null)
{
CommandManager.RequerySuggested -= handler;
}
}
}
}
internal static void AddWeakReferenceHandler(ref List<WeakReference> handlers, EventHandler handler)
{
AddWeakReferenceHandler(ref handlers, handler, -1);
}
internal static void AddWeakReferenceHandler(ref List<WeakReference> handlers, EventHandler handler, int defaultListSize)
{
if (handlers == null)
{
handlers = (defaultListSize > 0 ? new List<WeakReference>(defaultListSize) : new List<WeakReference>());
}
handlers.Add(new WeakReference(handler));
}
internal static void RemoveWeakReferenceHandler(List<WeakReference> handlers, EventHandler handler)
{
if (handlers != null)
{
for (int i = handlers.Count - 1; i >= 0; i--)
{
WeakReference reference = handlers[i];
EventHandler existingHandler = reference.Target as EventHandler;
if ((existingHandler == null) || (existingHandler == handler))
{
// Clean up old handlers that have been collected
// in addition to the handler that is to be removed.
handlers.RemoveAt(i);
}
}
}
}
}
I have then written a ListenOn extension method, that 'binds' the command to a property, and invokes its RaiseCanExecuteChanged:
public static class DelegateCommandExtensions
{
/// <summary>
/// Makes DelegateCommnand listen on PropertyChanged events of some object,
/// so that DelegateCommnand can update its IsEnabled property.
/// </summary>
public static DelegateCommand ListenOn<ObservedType, PropertyType>
(this DelegateCommand delegateCommand,
ObservedType observedObject,
Expression<Func<ObservedType, PropertyType>> propertyExpression,
Dispatcher dispatcher)
where ObservedType : INotifyPropertyChanged
{
//string propertyName = observedObject.GetPropertyName(propertyExpression);
string propertyName = NotifyPropertyChangedBaseExtensions.GetPropertyName(propertyExpression);
observedObject.PropertyChanged += (sender, e) =>
{
if (e.PropertyName == propertyName)
{
if (dispatcher != null)
{
ThreadTools.RunInDispatcher(dispatcher, delegateCommand.RaiseCanExecuteChanged);
}
else
{
delegateCommand.RaiseCanExecuteChanged();
}
}
};
return delegateCommand; //chain calling
}
/// <summary>
/// Makes DelegateCommnand listen on PropertyChanged events of some object,
/// so that DelegateCommnand can update its IsEnabled property.
/// </summary>
public static DelegateCommand<T> ListenOn<T, ObservedType, PropertyType>
(this DelegateCommand<T> delegateCommand,
ObservedType observedObject,
Expression<Func<ObservedType, PropertyType>> propertyExpression,
Dispatcher dispatcher)
where ObservedType : INotifyPropertyChanged
{
//string propertyName = observedObject.GetPropertyName(propertyExpression);
string propertyName = NotifyPropertyChangedBaseExtensions.GetPropertyName(propertyExpression);
observedObject.PropertyChanged += (object sender, PropertyChangedEventArgs e) =>
{
if (e.PropertyName == propertyName)
{
if (dispatcher != null)
{
ThreadTools.RunInDispatcher(dispatcher, delegateCommand.RaiseCanExecuteChanged);
}
else
{
delegateCommand.RaiseCanExecuteChanged();
}
}
};
return delegateCommand; //chain calling
}
}
You then need the following extension to NotifyPropertyChanged
/// <summary>
/// <see cref="http://dotnet.dzone.com/news/silverlightwpf-implementing"/>
/// </summary>
public static class NotifyPropertyChangedBaseExtensions
{
/// <summary>
/// Raises PropertyChanged event.
/// To use: call the extension method with this: this.OnPropertyChanged(n => n.Title);
/// </summary>
/// <typeparam name="T">Property owner</typeparam>
/// <typeparam name="TProperty">Type of property</typeparam>
/// <param name="observableBase"></param>
/// <param name="expression">Property expression like 'n => n.Property'</param>
public static void OnPropertyChanged<T, TProperty>(this T observableBase, Expression<Func<T, TProperty>> expression) where T : INotifyPropertyChangedWithRaise
{
observableBase.OnPropertyChanged(GetPropertyName<T, TProperty>(expression));
}
public static string GetPropertyName<T, TProperty>(Expression<Func<T, TProperty>> expression) where T : INotifyPropertyChanged
{
if (expression == null)
throw new ArgumentNullException("expression");
var lambda = expression as LambdaExpression;
MemberExpression memberExpression;
if (lambda.Body is UnaryExpression)
{
var unaryExpression = lambda.Body as UnaryExpression;
memberExpression = unaryExpression.Operand as MemberExpression;
}
else
{
memberExpression = lambda.Body as MemberExpression;
}
if (memberExpression == null)
throw new ArgumentException("Please provide a lambda expression like 'n => n.PropertyName'");
MemberInfo memberInfo = memberExpression.Member;
if (String.IsNullOrEmpty(memberInfo.Name))
throw new ArgumentException("'expression' did not provide a property name.");
return memberInfo.Name;
}
}
where INotifyPropertyChangedWithRaise is this (it estabilishes standard interface for raising NotifyPropertyChanged events):
public interface INotifyPropertyChangedWithRaise : INotifyPropertyChanged
{
void OnPropertyChanged(string propertyName);
}
Last piece of puzzle is this:
public class ThreadTools
{
public static void RunInDispatcher(Dispatcher dispatcher, Action action)
{
RunInDispatcher(dispatcher, DispatcherPriority.Normal, action);
}
public static void RunInDispatcher(Dispatcher dispatcher, DispatcherPriority priority, Action action)
{
if (action == null) { return; }
if (dispatcher.CheckAccess())
{
// we are already on thread associated with the dispatcher -> just call action
try
{
action();
}
catch (Exception ex)
{
//Log error here!
}
}
else
{
// we are on different thread, invoke action on dispatcher's thread
dispatcher.BeginInvoke(
priority,
(Action)(
() =>
{
try
{
action();
}
catch (Exception ex)
{
//Log error here!
}
})
);
}
}
}
This solution is a reduced version of the solution proposed by Tomáš Kafka(thanks to Tomas for describing his solution in detail)in this thread.
In Tomas's solution he had
1) DelegateCommand
2) CommandManagerHelper
3) DelegateCommandExtensions
4) NotifyPropertyChangedBaseExtensions
5) INotifyPropertyChangedWithRaise
6) ThreadTools
This solution has
1) DelegateCommand
2) DelegateCommandExtensions method and NotifyPropertyChangedBaseExtensions method in Delegate Command itself.
Note Since our wpf application follows MVVM pattern and we handle commands at viewmodel level which executes in UI thread we don't need to get the reference to UI disptacher.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;
using System.Windows.Input;
namespace ExampleForDelegateCommand
{
public class DelegateCommand : ICommand
{
public Predicate<object> CanExecuteDelegate { get; set; }
private List<INotifyPropertyChanged> propertiesToListenTo;
private List<WeakReference> ControlEvent;
public DelegateCommand()
{
ControlEvent= new List<WeakReference>();
}
public List<INotifyPropertyChanged> PropertiesToListenTo
{
get { return propertiesToListenTo; }
set
{
propertiesToListenTo = value;
}
}
private Action<object> executeDelegate;
public Action<object> ExecuteDelegate
{
get { return executeDelegate; }
set
{
executeDelegate = value;
ListenForNotificationFrom((INotifyPropertyChanged)executeDelegate.Target);
}
}
public static ICommand Create(Action<object> exec)
{
return new SimpleCommand { ExecuteDelegate = exec };
}
#region ICommand Members
public bool CanExecute(object parameter)
{
if (CanExecuteDelegate != null)
return CanExecuteDelegate(parameter);
return true; // if there is no can execute default to true
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
ControlEvent.Add(new WeakReference(value));
}
remove
{
CommandManager.RequerySuggested -= value;
ControlEvent.Remove(ControlEvent.Find(r => ((EventHandler) r.Target) == value));
}
}
public void Execute(object parameter)
{
if (ExecuteDelegate != null)
ExecuteDelegate(parameter);
}
#endregion
public void RaiseCanExecuteChanged()
{
if (ControlEvent != null && ControlEvent.Count > 0)
{
ControlEvent.ForEach(ce =>
{
if(ce.Target!=null)
((EventHandler) (ce.Target)).Invoke(null, EventArgs.Empty);
});
}
}
public DelegateCommand ListenOn<TObservedType, TPropertyType>(TObservedType viewModel, Expression<Func<TObservedType, TPropertyType>> propertyExpression) where TObservedType : INotifyPropertyChanged
{
string propertyName = GetPropertyName(propertyExpression);
viewModel.PropertyChanged += (PropertyChangedEventHandler)((sender, e) =>
{
if (e.PropertyName == propertyName) RaiseCanExecuteChanged();
});
return this;
}
public void ListenForNotificationFrom<TObservedType>(TObservedType viewModel) where TObservedType : INotifyPropertyChanged
{
viewModel.PropertyChanged += (PropertyChangedEventHandler)((sender, e) =>
{
RaiseCanExecuteChanged();
});
}
private string GetPropertyName<T, TProperty>(Expression<Func<T, TProperty>> expression) where T : INotifyPropertyChanged
{
var lambda = expression as LambdaExpression;
MemberInfo memberInfo = GetmemberExpression(lambda).Member;
return memberInfo.Name;
}
private MemberExpression GetmemberExpression(LambdaExpression lambda)
{
MemberExpression memberExpression;
if (lambda.Body is UnaryExpression)
{
var unaryExpression = lambda.Body as UnaryExpression;
memberExpression = unaryExpression.Operand as MemberExpression;
}
else
memberExpression = lambda.Body as MemberExpression;
return memberExpression;
}
}}
Explanation of the solution:
Normally when we bind a UI element(Button)to the ICommand implementation the WPF Button registers for a Event "CanExecuteChanged" in ICommand implementation .If your Icommand implementation for "CanExecuteChanged" hook to the CommandManager's RequesySuggest event(read this article http://joshsmithonwpf.wordpress.com/2008/06/17/allowing-commandmanager-to-query-your-icommand-objects/) then when ever CommandManager detects conditions that might change the ability of a command to execute(changes like Focus shifts and some keyboard events) , CommandManager’s RequerySuggested event occurs which in turn will cause Button'e delegate to be called since we hooked the buttos's delgate to CommandManager’s RequerySuggested in the implementation of "CanExecuteChanged" in our DelegateCommand .
But the problem is that ComandManager is not able to always detect the changes. Hence the solution it to raise "CanExecuteChanged" when our command implementation(DelegateCommand) detects there is a change.Normally when we declare the delagate for ICommand's CanExecute in our viewmodel we bind to properties declared in our viewmodel and our ICommand implementation can listen for "propertychanged" events on these properties. Thats what the "ListenForNotificationFrom" method of the DelegateCommand does. In case the client code does not register for specific property changes the DelegateCommand by defaults listens to any property change on the view model where command is declared and defined.
"ControlEvent" in DelegateCommand which is list of EventHandler that stores the Button's
"CanExecuteChange EventHandler" is declared as weak reference to avoid memory leaks.
How will ViewModel use this DelegateCommand
There are 2 ways to use it.
(the second usage is more specific to the properties that you want the Command to listen to.
delegateCommand = new DelegateCommand
{
ExecuteDelegate = Search,
CanExecuteDelegate = (r) => !IsBusy
};
anotherDelegateCommand = new DelegateCommand
{
ExecuteDelegate = SearchOne,
CanExecuteDelegate = (r) => !IsBusyOne
}.ListenOn(this, n => n.IsBusyOne);
A detailed ViewModel
public class ExampleViewModel
{
public SearchViewModelBase()
{
delegateCommand = new DelegateCommand
{
ExecuteDelegate = Search,
CanExecuteDelegate = (r) => !IsBusy
};
anotherDelegateCommand = new DelegateCommand
{
ExecuteDelegate = SearchOne,
CanExecuteDelegate = (r) => !IsBusyOne
}.ListenOn(this, n => n.IsBusyOne);
}
private bool isBusy;
public virtual bool IsBusy
{
get { return isBusy; }
set
{
if (isBusy == value) return;
isBusy = value;
NotifyPropertyChanged(MethodBase.GetCurrentMethod());
}
}
private bool isBusyOne;
public virtual bool IsBusyOne
{
get { return isBusyOne; }
set
{
if (isBusyOne == value) return;
isBusyOne = value;
NotifyPropertyChanged(MethodBase.GetCurrentMethod());
}
}
private void Search(object obj)
{
IsBusy = true;
new SearchService().Search(Callback);
}
public void Callback(ServiceResponse response)
{
IsBusy = false;
}
private void Search(object obj)
{
IsBusyOne = true;
new SearchService().Search(CallbackOne);
}
public void CallbackOne(ServiceResponse response)
{
IsBusyOne = false;
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void NotifyPropertyChanged(MethodBase methodBase)
{
string methodName = methodBase.Name;
if (!methodName.StartsWith("set_"))
{
var ex = new ArgumentException("MethodBase must refer to a Property Setter method.");
throw ex;
}
NotifyPropertyChanged(methodName.Substring(4));
}
}
Tomas has a nice solution, but pls note there's a serious bug in that the CanExecute will not always fire when bound to a Button due to this :
// Call the handlers that we snapshotted
for (int i = 0; i < count; i++)
{
EventHandler handler = callees[i];
handler(null, EventArgs.Empty);
}
The 'null' parameter passed in causes issues with the CanExecuteChangedEventManager (used by the WPF Button class to listen to changes on any Command bound to it). Specifically, the CanExecuteChangedEventManager maintains a collection of weak events that need to be invoked to determine if the command Can-Execute() but this collection is keyed by the 'sender'.
The fix is simple and works for me - change the signature to
internal static void CallWeakReferenceHandlers(ICommand sender, List<WeakReference> handlers)
{
....
handler(sender, EventArgs.Empty);
}
Sorry I haven't described it too well - in a bit of a rush to catch up with my dev now after taking a few hours to figure this out !
I would suggest looking into ReactiveUI and specifically at the ICommand implementation it provides, ReactiveCommand. It uses a different approach than DelegateCommand/RelayCommand which are implemented with delegates for CanExecute that must be actively evaluated. ReactiveCommand's value for CanExecute is pushed using IObservables.
is there a way I can make CommandManager.InvalidateRequerySuggested() do it's job faster?
Yes, there is way to make it work faster!
Implement Command to keep / cache CanExecuteState in a boolean variable.
Implement RaiseCanExecuteChanged method to recalculate CanExecuteState and if it really changed to raise CanExecuteChanged event.
Implement CanExecute method to simply return CanExecuteState.
When InvalidateRequerySuggested method is invoked Command subscribers will only read CanExecuteState variable by invoking CanExecute method and check if it changed or not. That's almost zero overhead. All Commands will be disabled / enabled almost the same time.
All work will be done in RaiseCanExecuteChanged method that will be called only once for a Command and only for a limited set of Commands.
Try writing your own binding that calls your RaiseCanExecuteChanged() within converts? it is easier
Just to clarify:
You want to fire an update of CanExecute when Command property changed
Create your own binding class that detect changes in the Command property and then calls RaiseCanExecuteChanged()
Use this binding in CommandParameter
Worked for me.