I have
<Hyperlink attachedProperty:HyperlinkExtensions.IsExternal="true">
<Hyperlink.NavigateUri>
<Binding Path="Name" StringFormat="http://anycoolsite.net/tag={0}" />
</Hyperlink.NavigateUri>
<TextBlock>
<TextBlock.Text>
<Binding Path="Title" StringFormat="http://anycoolsite.net/tag={0}" />
</TextBlock.Text>
</TextBlock>
</Hyperlink>
and an attached property for that
public class HyperlinkExtensions
{
public static bool GetIsExternal(DependencyObject obj)
{
return (bool)obj.GetValue(IsExternalProperty);
}
public static void SetIsExternal(DependencyObject obj, bool value)
{
obj.SetValue(IsExternalProperty, value);
}
public static readonly DependencyProperty IsExternalProperty =
DependencyProperty.RegisterAttached("IsExternal", typeof(bool), typeof(HyperlinkExtensions), new UIPropertyMetadata(false, OnIsExternalChanged));
private static void OnIsExternalChanged(object sender, DependencyPropertyChangedEventArgs args)
{
var hyperlink = sender as Hyperlink;
if ((bool)args.NewValue)
hyperlink.RequestNavigate += Hyperlink_RequestNavigate;
else
hyperlink.RequestNavigate -= Hyperlink_RequestNavigate;
}
private static void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
}
When I try to get an url from e, I only have a bound Name part, which is in {0}.
How to get the formatted one (http://anycoolsite.net/anyboundname)?
For testblock text the binding and formatting works as expected
OK, changed my extension this way:
public class HyperlinkExtensions
{
public static string GetUrlFormat(DependencyObject obj)
{
return (string)obj.GetValue(UrlFormatProperty);
}
public static void SetUrlFormat(DependencyObject obj, string value)
{
obj.SetValue(UrlFormatProperty, value);
}
public static readonly DependencyProperty UrlFormatProperty =
DependencyProperty.RegisterAttached("UrlFormat", typeof(string), typeof(HyperlinkExtensions), new UIPropertyMetadata(string.Empty, OnUrlFormatChanged));
private static void OnUrlFormatChanged(object sender, DependencyPropertyChangedEventArgs args)
{
var hyperlink = sender as Hyperlink;
if (!string.IsNullOrEmpty((string) args.NewValue))
{
hyperlink.NavigateUri = new Uri(string.Format((string) args.NewValue, hyperlink.NavigateUri));
hyperlink.RequestNavigate -= Hyperlink_RequestNavigate;
hyperlink.RequestNavigate += Hyperlink_RequestNavigate;
}
else
{
hyperlink.RequestNavigate -= Hyperlink_RequestNavigate;
}
}
private static void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
}
Usage:
<Hyperlink attachedProperty:HyperlinkExtensions.UrlFormat="https://anycoolsite.com/products/{0}" ToolTip="Click to open in browser">
<Hyperlink.NavigateUri>
<Binding Path="ProductName"/>
</Hyperlink.NavigateUri>
<TextBlock Text="{Binding Title}"/>
</Hyperlink>
Related
I have a behavior for Textbox:
// C#
public class DigitsOnlyBehavior : Behavior<UIElement>
{
public static readonly DependencyProperty IsDigitOnlyProperty =
DependencyProperty.RegisterAttached("IsDigitOnly",
typeof(bool), typeof(DigitsOnlyBehavior),
new FrameworkPropertyMetadata(false, OnIsDigitOnlyChanged));
public static bool GetIsDigitOnly(Control obj)
{
return (bool)obj.GetValue(IsDigitOnlyProperty);
}
public static void SetIsDigitOnly(Control obj, bool value)
{
obj.SetValue(IsDigitOnlyProperty, value);
}
private static void OnIsDigitOnlyChanged(object sender, DependencyPropertyChangedEventArgs e)
{
// ignoring error checking
var textBox = (TextBox)sender;
var isDigitOnly = (bool)(e.NewValue);
if (isDigitOnly)
textBox.PreviewTextInput += BlockNonDigitCharacters;
else
textBox.PreviewTextInput -= BlockNonDigitCharacters;
}
}
In xaml I do this:
<!-- XAML -->
<TextBox local:DigitsOnlyBehavior.IsDigitOnly="true" />
Question is why I must to type string false or true, instead choose from list False or True like for let's say IsEnabled property? It's possible to change that?
I have created an attched property to bind the MouseUpCommand and the MouseDownCommand to my ViewModel. While the MouseDownCommand fires as it should, the MouseUpCommand only fires sporadically. Does anyone know what I am doing wrong?
Here is my XAML:
<ItemsControl ItemsSource="{Binding Page.Collection}" Grid.Row="1" Grid.Column="1"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas IsItemsHost="True" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Background="Blue"
local:MouseBehaviour.MouseUpCommand="{Binding ViewModel.MouseUpCommand}"
local:MouseBehaviour.MouseDownCommand="{Binding ViewModel.MouseDownCommand}"
local:MouseBehaviour.MouseMoveCommand="{Binding ViewModel.MouseMoveCommand}">
</Canvas>
Here is the MouseBehavior:
public class MouseBehaviour
{
#region MouseUp
public static readonly DependencyProperty MouseUpCommandProperty =
DependencyProperty.RegisterAttached("MouseUpCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseUpCommandChanged)));
private static void MouseUpCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)d;
element.PreviewMouseUp += element_MouseUp;
}
static void element_MouseUp(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
ICommand command = GetMouseUpCommand(element);
command.Execute(e);
}
public static void SetMouseUpCommand(UIElement element, ICommand value)
{
element.SetValue(MouseUpCommandProperty, value);
}
public static ICommand GetMouseUpCommand(UIElement element)
{
return (ICommand)element.GetValue(MouseUpCommandProperty);
}
#endregion
#region MouseDown
public static readonly DependencyProperty MouseDownCommandProperty =
DependencyProperty.RegisterAttached("MouseDownCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseDownCommandChanged)));
private static void MouseDownCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)d;
element.PreviewMouseDown += element_MouseDown;
}
static void element_MouseDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
ICommand command = GetMouseDownCommand(element);
command.Execute(e);
}
public static void SetMouseDownCommand(UIElement element, ICommand value)
{
element.SetValue(MouseDownCommandProperty, value);
}
public static ICommand GetMouseDownCommand(UIElement element)
{
return (ICommand)element.GetValue(MouseDownCommandProperty);
}
#endregion
}
I have tried using the normal mouse commands as well as the preview mouse commands. No difference.
If I break the MVVM pattern and just add events to the code behind the program works flawlessly:
private void ItemsControl_MouseUp(object sender, MouseButtonEventArgs e)
{
mainViewModel.ViewModel.MouseUp(e);
}
private void ItemsControl_MouseMove(object sender, MouseEventArgs e)
{
mainViewModel.ViewModel.MouseMove(e);
}
private void ItemsControl_MouseDown(object sender, MouseButtonEventArgs e)
{
mainViewModel.ViewModel.MouseDown(e);
}
I am Using MVVM with WPF. i am halted in a way, i want your views about this. i am firing Mouse Events using MouseBehaviour.cs class , Is there any other way to handle Mouse Events in MVVM WPF
using System.Windows;
using System.Windows.Input;
namespace Lovatts.MouseBehaviours
{
public class MouseBehaviour
{
#region MouseUp
public static readonly DependencyProperty MouseUpCommandProperty =
DependencyProperty.RegisterAttached("MouseUpCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseUpCommandChanged)));
private static void MouseUpCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)d;
element.MouseUp += element_MouseUp;
}
static void element_MouseUp(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
ICommand command = GetMouseUpCommand(element);
command.Execute(e);
}
public static void SetMouseUpCommand(UIElement element, ICommand value)
{
element.SetValue(MouseUpCommandProperty, value);
}
public static ICommand GetMouseUpCommand(UIElement element)
{
return (ICommand)element.GetValue(MouseUpCommandProperty);
}
#endregion
#region MouseDown
public static readonly DependencyProperty MouseDownCommandProperty =
DependencyProperty.RegisterAttached("MouseDownCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseDownCommandChanged)));
private static void MouseDownCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)d;
element.MouseDown += element_MouseDown;
}
static void element_MouseDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
ICommand command = GetMouseDownCommand(element);
command.Execute(e);
}
public static void SetMouseDownCommand(UIElement element, ICommand value)
{
element.SetValue(MouseDownCommandProperty, value);
}
public static ICommand GetMouseDownCommand(UIElement element)
{
return (ICommand)element.GetValue(MouseDownCommandProperty);
}
#endregion
#region MouseLeave
public static readonly DependencyProperty MouseLeaveCommandProperty =
DependencyProperty.RegisterAttached("MouseLeaveCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseLeaveCommandChanged)));
private static void MouseLeaveCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)d;
element.MouseLeave += new MouseEventHandler(element_MouseLeave);
}
static void element_MouseLeave(object sender, MouseEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
ICommand command = GetMouseLeaveCommand(element);
command.Execute(e);
}
public static void SetMouseLeaveCommand(UIElement element, ICommand value)
{
element.SetValue(MouseLeaveCommandProperty, value);
}
public static ICommand GetMouseLeaveCommand(UIElement element)
{
return (ICommand)element.GetValue(MouseLeaveCommandProperty);
}
#endregion
#region MouseLeftButtonDown
public static readonly DependencyProperty MouseLeftButtonDownCommandProperty =
DependencyProperty.RegisterAttached("MouseLeftButtonDownCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseLeftButtonDownCommandChanged)));
private static void MouseLeftButtonDownCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)d;
element.MouseLeftButtonDown += element_MouseLeftButtonDown;
}
static void element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
ICommand command = GetMouseLeftButtonDownCommand(element);
command.Execute(e);
}
public static void SetMouseLeftButtonDownCommand(UIElement element, ICommand value)
{
element.SetValue(MouseLeftButtonDownCommandProperty, value);
}
public static ICommand GetMouseLeftButtonDownCommand(UIElement element)
{
return (ICommand)element.GetValue(MouseLeftButtonDownCommandProperty);
}
#endregion
#region MouseLeftButtonUp
public static readonly DependencyProperty MouseLeftButtonUpCommandProperty =
DependencyProperty.RegisterAttached("MouseLeftButtonUpCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseLeftButtonUpCommandChanged)));
private static void MouseLeftButtonUpCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)d;
element.MouseLeftButtonUp += element_MouseLeftButtonUp;
}
static void element_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
ICommand command = GetMouseLeftButtonUpCommand(element);
command.Execute(e);
}
public static void SetMouseLeftButtonUpCommand(UIElement element, ICommand value)
{
element.SetValue(MouseLeftButtonUpCommandProperty, value);
}
public static ICommand GetMouseLeftButtonUpCommand(UIElement element)
{
return (ICommand)element.GetValue(MouseLeftButtonUpCommandProperty);
}
#endregion
#region MouseMove
public static readonly DependencyProperty MouseMoveCommandProperty =
DependencyProperty.RegisterAttached("MouseMoveCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseMoveCommandChanged)));
private static void MouseMoveCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)d;
element.MouseMove += new MouseEventHandler(element_MouseMove);
}
static void element_MouseMove(object sender, MouseEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
ICommand command = GetMouseMoveCommand(element);
command.Execute(e);
}
public static void SetMouseMoveCommand(UIElement element, ICommand value)
{
element.SetValue(MouseMoveCommandProperty, value);
}
public static ICommand GetMouseMoveCommand(UIElement element)
{
return (ICommand)element.GetValue(MouseMoveCommandProperty);
}
#endregion
#region MouseRightButtonDown
public static readonly DependencyProperty MouseRightButtonDownCommandProperty =
DependencyProperty.RegisterAttached("MouseRightButtonDownCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseRightButtonDownCommandChanged)));
private static void MouseRightButtonDownCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)d;
element.MouseRightButtonDown += element_MouseRightButtonDown;
}
static void element_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
ICommand command = GetMouseRightButtonDownCommand(element);
command.Execute(e);
}
public static void SetMouseRightButtonDownCommand(UIElement element, ICommand value)
{
element.SetValue(MouseRightButtonDownCommandProperty, value);
}
public static ICommand GetMouseRightButtonDownCommand(UIElement element)
{
return (ICommand)element.GetValue(MouseRightButtonDownCommandProperty);
}
#endregion
#region MouseRightButtonUp
public static readonly DependencyProperty MouseRightButtonUpCommandProperty =
DependencyProperty.RegisterAttached("MouseRightButtonUpCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseRightButtonUpCommandChanged)));
private static void MouseRightButtonUpCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)d;
element.MouseRightButtonUp += element_MouseRightButtonUp;
}
static void element_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
ICommand command = GetMouseRightButtonUpCommand(element);
command.Execute(e);
}
public static void SetMouseRightButtonUpCommand(UIElement element, ICommand value)
{
element.SetValue(MouseRightButtonUpCommandProperty, value);
}
public static ICommand GetMouseRightButtonUpCommand(UIElement element)
{
return (ICommand)element.GetValue(MouseRightButtonUpCommandProperty);
}
#endregion
#region MouseWheel
public static readonly DependencyProperty MouseWheelCommandProperty =
DependencyProperty.RegisterAttached("MouseWheelCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseWheelCommandChanged)));
private static void MouseWheelCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)d;
element.MouseWheel += new MouseWheelEventHandler(element_MouseWheel);
}
static void element_MouseWheel(object sender, MouseWheelEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
ICommand command = GetMouseWheelCommand(element);
command.Execute(e);
}
public static void SetMouseWheelCommand(UIElement element, ICommand value)
{
element.SetValue(MouseWheelCommandProperty, value);
}
public static ICommand GetMouseWheelCommand(UIElement element)
{
return (ICommand)element.GetValue(MouseWheelCommandProperty);
}
#endregion
}
}
Here is my Xaml Code
<Window x:Class="Lovatts.MouseBehaviours.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MouseBehaviours="clr-namespace:Lovatts.MouseBehaviours" x:Name="RibbonWindow"
Width="640" Height="480" DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid>
<Rectangle Width="100" Height="100" Fill="Blue" MouseBehaviours:MouseBehaviour.MouseUpCommand="{Binding MouseUpCommand}">
</Rectangle>
<Rectangle Width="100" Height="100" x:Name="Rectangle" Fill="Red" MouseBehaviours:MouseBehaviour.MouseUpCommand="{Binding MouseUpCommand}" Margin="75,109,457,241">
</Rectangle>
</Grid>
</Window>
MainViewModel.cs
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Windows;
namespace MouseBehaviours.ViewModel
{
public class MainViewModel : ViewModelBase
{
private RelayCommand _mouseUpCommand;
public RelayCommand MouseUpCommand
{
get
{
if (_mouseUpCommand == null) return _mouseUpCommand = new RelayCommand(param => ExecuteMouseUp((MouseEventArgs)param));
return _mouseUpCommand;
}
set { _mouseUpCommand = value; }
}
private void ExecuteMouseUp(MouseEventArgs e)
{
Console.WriteLine("Mouse Up : " + e.GetPosition((IInputElement)e.Source));
}
}
public class RelayCommand : ICommand
{
#region Fields
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
private string _displayText;
public static List<string> Log = new List<string>();
private Action<object> action;
#endregion // Fields
#region Constructors
/// <summary>
/// Creates a new command that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
/// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
: this(execute, canExecute, "")
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute, string displayText)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
_displayText = displayText;
}
public string DisplayText
{
get { return _displayText; }
set { _displayText = value; }
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion // ICommand Members
}
//class MainViewModel
//{
//}
}
Instead of writing you own behavior and calling commands from it, you can leverage the EventTriggers and Interactivity to bind the event action to the command.
here is simple example of doing it
http://www.c-sharpcorner.com/Blogs/11789/example-of-eventtrigger-in-mvvm-application.aspx
as described in the example if you want to fire command on MouseUp event on your rectangle you can just do:
<Rectangle >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseUp">
<i:InvokeCommandAction Command="{Binding MyCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle >
#harjeet you need to use the below structure for Image MouseUp i.e instead of using "Image" try to use "Hyperlink command property" in TextBlock:
<TextBlock Panel.ZIndex="990"
Canvas.Right="30"
Canvas.Left="498"
Canvas.Top="4">
<Hyperlink TextDecorations="None"
Command="{Binding CloseLoginSettingPopup}">
<Image Cursor="Hand"
x:Name="Pop2"
Source="/img/close1.png"
Height="40"
Width="40"
Panel.ZIndex="990"
Canvas.Right="30"
Canvas.Left="498"
Canvas.Top="4" />
</Hyperlink>
</TextBlock>
https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.mouseaction
<Rectangle>
<Rectangle.InputBindings>
<MouseBinding MouseAction="LeftClick" Command="{Binding MyCommand}" />
</Rectangle.InputBindings>
</Rectangle>
I try to use this xaml, to apply an event to command binding:
<telerik:RadGridView x:Name="xRadGridView"
prismcommands:SelectionChangedCommand.Command="{Binding SelectPersonCommand}"
ItemsSource="{Binding GridItems, Mode=TwoWay}">
</telerik:RadGridView>
I get the error:
'SelectionChangedCommand.Command' property is read-only and cannot be
set from markup.
I can bind to prismcommands:RowEditEndedCommand.Command with no problem.
Is there any chance to bind to SelectionChangedCommand.Command?
I use the same PrismCommands in a Silverlight project and it works there.
namespace RadEventToCommand.WPF.PrismCommands
{
public class RowEditEndedCommandBehavior : CommandBehaviorBase<RadGridView>
{
public RowEditEndedCommandBehavior(RadGridView gridView)
: base(gridView)
{
gridView.RowEditEnded +=new EventHandler<GridViewRowEditEndedEventArgs>(gridView_RowEditEnded);
}
void gridView_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
{
CommandParameter = e;
ExecuteCommand();
}
}
}
--
namespace RadEventToCommand.WPF.PrismCommands
{
public static class SelectionChangedCommand
{
private static readonly DependencyProperty SelectionChangedCommandBehaviorProperty
= DependencyProperty.RegisterAttached(
"SelectionChangedCommandBehavior",
typeof(SelectionChangedCommandBehavior),
typeof(SelectionChangedCommand),
null);
public static readonly DependencyProperty CommandProperty
= DependencyProperty.RegisterAttached(
"Command",
typeof(ICommand),
typeof(SelectionChangedCommand),
new PropertyMetadata(OnSetCommandCallback));
public static readonly DependencyProperty CommandParameterProperty
= DependencyProperty.RegisterAttached(
"CommandParameter",
typeof(object),
typeof(SelectionChangedCommand),
new PropertyMetadata(OnSetCommandParameterCallback));
public static ICommand GetCommand(RadGridView gridView)
{
return gridView.GetValue(CommandProperty) as ICommand;
}
public static void SetCommandParameter(RadGridView gridView, object parameter)
{
gridView.SetValue(CommandParameterProperty, parameter);
}
public static object GetCommandParameter(RadGridView gridView)
{
return gridView.GetValue(CommandParameterProperty);
}
private static void OnSetCommandCallback
(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
RadGridView gridView = dependencyObject as RadGridView;
if (gridView != null)
{
SelectionChangedCommandBehavior behavior = GetOrCreateBehavior(gridView);
behavior.Command = e.NewValue as ICommand;
}
}
private static void OnSetCommandParameterCallback
(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
RadGridView gridView = dependencyObject as RadGridView;
if (gridView != null)
{
SelectionChangedCommandBehavior behavior = GetOrCreateBehavior(gridView);
behavior.CommandParameter = e.NewValue;
}
}
private static SelectionChangedCommandBehavior GetOrCreateBehavior(RadGridView gridView)
{
SelectionChangedCommandBehavior behavior =
gridView.GetValue(SelectionChangedCommandBehaviorProperty) as SelectionChangedCommandBehavior;
if (behavior == null)
{
behavior = new SelectionChangedCommandBehavior(gridView);
gridView.SetValue(SelectionChangedCommandBehaviorProperty, behavior);
}
return behavior;
}
}
}
--
namespace RadEventToCommand.WPF.PrismCommands
{
public class RowEditEndedCommandBehavior : CommandBehaviorBase<RadGridView>
{
public RowEditEndedCommandBehavior(RadGridView gridView)
: base(gridView)
{
gridView.RowEditEnded +=new EventHandler<GridViewRowEditEndedEventArgs>(gridView_RowEditEnded);
}
void gridView_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
{
CommandParameter = e;
ExecuteCommand();
}
}
}
--
namespace RadEventToCommand.WPF.PrismCommands
{
public static class RowEditEndedCommand
{
private static DependencyProperty RowEditEndedCommandBehaviorProperty
= DependencyProperty.RegisterAttached(
"RowEditEndedCommandBehavior",
typeof(RowEditEndedCommandBehavior),
typeof(RowEditEndedCommand),
null);
public static DependencyProperty CommandProperty
= DependencyProperty.RegisterAttached(
"Command",
typeof(ICommand),
typeof(RowEditEndedCommand),
new PropertyMetadata(OnSetCommandCallback));
public static DependencyProperty CommandParameterProperty
= DependencyProperty.RegisterAttached(
"CommandParameter",
typeof(object),
typeof(RowEditEndedCommand),
new PropertyMetadata(OnSetCommandParameterCallback));
public static ICommand GetCommand(RadGridView gridView)
{
return gridView.GetValue(CommandProperty) as ICommand;
}
public static void SetCommand(RadGridView gridView, object parameter)
{
gridView.SetValue(CommandProperty, parameter);
}
public static void SetCommandParameter(RadGridView gridView, object parameter)
{
gridView.SetValue(CommandParameterProperty, parameter);
}
public static object GetCommandParameter(RadGridView gridView)
{
return gridView.GetValue(CommandParameterProperty);
}
private static void OnSetCommandCallback
(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
RadGridView gridView = dependencyObject as RadGridView;
if (gridView != null)
{
RowEditEndedCommandBehavior behavior = GetOrCreateBehavior(gridView);
behavior.Command = e.NewValue as ICommand;
}
}
private static void OnSetCommandParameterCallback
(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
RadGridView gridView = dependencyObject as RadGridView;
if (gridView != null)
{
RowEditEndedCommandBehavior behavior = GetOrCreateBehavior(gridView);
behavior.CommandParameter = e.NewValue;
}
}
private static RowEditEndedCommandBehavior GetOrCreateBehavior(RadGridView gridView)
{
RowEditEndedCommandBehavior behavior =
gridView.GetValue(RowEditEndedCommandBehaviorProperty) as RowEditEndedCommandBehavior;
if (behavior == null)
{
behavior = new RowEditEndedCommandBehavior(gridView);
gridView.SetValue(RowEditEndedCommandBehaviorProperty, behavior);
}
return behavior;
}
}
}
I had the source for the behavior copied over from a Silverlight project. It worked there. For some reason in WPF I need the additional method in SelectionChangedCommand
public static void SetCommand(RadGridView gridView, object parameter)
{
gridView.SetValue(CommandProperty, parameter);
}
I copied the code over to check if I could use a common codebase for Silverlight and WPF.
For the RadGridView, we are using the Interaction Triggers. The below code works for us.
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectedItemChangedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
What is going on here, and how to do a workaround?
Press MyToggleButton. Now it looks like instantly pressed (checked).
At the same time MyToggleButton_Checked handler disables the MyToggleButton.
Now press non-toggle Button 'Enable'. What we see? The MyToggleButton looks like it is enabled. OK. But wait, now it looks like non-pressed! Why?!
XAML:
<StackPanel>
<ToggleButton x:Name="MyToggleButton" Content="MyToggleButton" Checked="MyToggleButton_Checked"/>
<TextBlock Text="{Binding IsChecked, ElementName=MyToggleButton}" Margin="0,4"/>
<Button Name="EnableButton" Content="Enable" Click="EnableButton_Click"/>
<Button Name="DisableButton" Content="Disable" Click="DisableButton_Click"/>
</StackPanel>
Code-behind:
void MyToggleButton_Checked(object sender, RoutedEventArgs e)
{
MyToggleButton.IsEnabled = false;
}
void EnableButton_Click(object sender, RoutedEventArgs e)
{
MyToggleButton.IsEnabled = true;
}
void DisableButton_Click(object sender, RoutedEventArgs e)
{
MyToggleButton.IsEnabled = false;
}
UPD:
The only possible workaround on the moment is:
void EnableButton_Click(object sender, RoutedEventArgs e)
{
MyToggleButton.IsEnabled = true;
var controlTemplate = MyToggleButton.Template;
var buttonChrome = (Microsoft.Windows.Themes.ButtonChrome)controlTemplate.FindName("Chrome", MyToggleButton);
buttonChrome.RenderPressed = false;
buttonChrome.RenderPressed = true;
}
Are there any others?
UPD2:
Another workaround is:
void EnableButton_Click(object sender, RoutedEventArgs e)
{
MyToggleButton.IsEnabled = true;
var controlTemplate = MyToggleButton.Template;
MyToggleButton.Template = null;
MyToggleButton.Template = controlTemplate;
}
But the control is flickering at the moment of template substitution.
That's my solution for the problem, here is the video, the usage:
<StackPanel>
<ToggleButton x:Name="MyToggleButton" Content="MyToggleButton" Checked="MyToggleButton_Checked"
l:CorrectToggleButtonCheckedEnableBehavior.IsActive="True"/>
<Button Name="EnableButton" Content="Enable" Click="EnableButton_Click"/>
</StackPanel>
and the attached behavior source code:
public static class CorrectToggleButtonCheckedEnableBehavior
{
public static bool GetIsActive(ToggleButton toggleButton)
{
return (bool)toggleButton.GetValue(IsActiveProperty);
}
public static void SetIsActive(ToggleButton toggleButton, bool value)
{
toggleButton.SetValue(IsActiveProperty, value);
}
public static readonly DependencyProperty IsActiveProperty =
DependencyProperty.RegisterAttached("IsActive", typeof(bool), typeof(CorrectToggleButtonCheckedEnableBehavior),
new UIPropertyMetadata(false, OnIsActiveChanged));
static void OnIsActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var toggleButton = (ToggleButton)d;
if (GetIsActive(toggleButton))
{
toggleButton.IsEnabledChanged += new DependencyPropertyChangedEventHandler(ToggleButton_IsEnabledChanged);
}
else
{
toggleButton.IsEnabledChanged -= new DependencyPropertyChangedEventHandler(ToggleButton_IsEnabledChanged);
}
}
static void ToggleButton_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var toggleButton = (ToggleButton)sender;
if (toggleButton.IsEnabled && (toggleButton.IsChecked ?? false))
{
Invalidate(toggleButton);
}
}
static void Invalidate(ToggleButton toggleButton)
{
var controlTemplate = toggleButton.Template;
toggleButton.Template = null;
toggleButton.Template = controlTemplate;
}
}