I'm trying to execute an ICommand (AsyncRelayCommand) on the loaded event of my UserControl using the Microsoft.Xaml.Behaviors.Wpf library.
<UserControl x:Class="..."
...
xmlns:Behaviors="http://schemas.microsoft.com/xaml/behaviors"
DataContext="{Binding ViewModel, Source={StaticResource ViewModelLocator}}">
<Behaviors:Interaction.Triggers>
<Behaviors:EventTrigger EventName="Loaded">
<Behaviors:InvokeCommandAction Command="{Binding LoadCommand}" />
</Behaviors:EventTrigger>
</Behaviors:Interaction.Triggers>
...
</UserControl>
The command property is called correctly, the Command class is created and it also calls correctly the CanExecute method (which returns true).
public override ICommand LoadCommand
{
get { return new AsyncRelayCommand(
async () => { /*never executed*/ },
() => return !this.IsLoading); // canExecute logic executed (true)
}
}
But it never calls the Execute method.
I believe I need somehow tell that I want the UserControl's event, but all examples are looking the same as mine.
I believe the design as described, does not adhere to separation of concerns between the user control and an external viewmodel.
I would instead use the Loaded event of the UserControl and create a specific dependency property on the control which the user will have bound the ICommand to when wiring up the control.
Example
#region public ICommand LoadCommand
/// <summary></summary>
public ICommand LoadCommand
{
get => GetValue(LoadCommandProperty) as ICommand;
set => SetValue(LoadCommandProperty, value);
}
/// <summary>
/// Identifies the LoadCommand dependency property.
/// </summary>
public static readonly DependencyProperty LoadCommandProperty =
DependencyProperty.Register(
"LoadCommand",
typeof(ICommand),
typeof(MainUserControl),
new PropertyMetadata(null));
#endregion public ICommand LoadCommand
private void MainUserControl_OnLoaded(object sender, RoutedEventArgs e)
=> LoadCommand?.Execute(null);
Related
Hi I am having trouble with WPF creating a RoutedUICommand that works similar to an ApplicationCommand. Ideally I want to have a single 'Duplicate' MenuItem that works with two different UserControls. Whether or not the MenuItem is active will depend on which UserControl is selected and if the UserControl contains a selected object (the same way cut, copy and past work on different textboxes, etc).
As per this StackOverflow, question I have created the static class below:
namespace App.Classes
{
public static class myCommands
{
private static readonly RoutedUICommand _duplicateCmd = new RoutedUICommand("Duplicate", "Duplicate", typeof(myCommands));
public static RoutedUICommand Duplicate
{
get { return _duplicateCmd; }
}
}
}
In the main Window's XAML file I have added a reference to the class's namespace and a KeyBinding to the command.
<Window x:Class="GUI.App.Views.Windows.wndMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:classes="clr-namespace:App.Classes">
<Window.InputBindings>
<KeyBinding Key="D" Modifiers="Control" Command="{Binding classes:myCommands.Duplicate}" />
</Window.InputBindings>
In the MenuItem I have added a Command binding to the 'Duplicate' command
<MenuItem Name="mnCopy" Header="Copy" Command="ApplicationCommands.Copy" InputGestureText="Ctrl+C" />
<MenuItem Name="mnDuplicate" Header="Duplicate" Command="{Binding classes:myCommands.Duplicate}" InputGestureText="Ctrl+D"/>
And finally in each of my UserControls I have added CommandBindings to the 'Duplicate' and other ApplicationCommands.
<UserControl.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy" Executed="CommandBinding_Copy" CanExecute="CommandBindingCanExecute_Copy" />
<CommandBinding Command="classes:myCommands.Duplicate" Executed="CommandBinding_DuplicateControls" CanExecute="CommandBindingCanExecute_DuplicateControls" />
</UserControl.CommandBindings>
The code-behind for my Executed and CanExecute for each of the UserControls is
/// <summary>
///
/// </summary>
public void CommandBinding_Copy(Object sender, ExecutedRoutedEventArgs e)
{
_viewModel.Copy();
}
/// <summary>
///
/// </summary>
public void CommandBinding_Duplicate(Object sender, ExecutedRoutedEventArgs e)
{
_viewModel.Duplicate();
}
/// <summary>
///
/// </summary>
public void CommandBindingCanExecute_Copy(Object sender, CanExecuteRoutedEventArgs e)
{
_viewModel.CanExecute_Copy();
}
/// <summary>
///
/// </summary>
public void CommandBindingCanExecute_Duplicate(Object sender, CanExecuteRoutedEventArgs e)
{
_viewModel.CanExecute_Duplicate();
}
This works perfectly for my ApplicationCommands and the appropriate Executed and CanExecute get called depending upon which UserControl is selected. But I can't get it to work with my 'Duplicate' RoutedUICommand. Does anybody know what I'm doing wrong or am missing? Will be grateful for any help anyone can give.
You're binding to a static property via Path and that won't work. Change Binding to x:Static and should work
<MenuItem Name="mnCopy" Command="ApplicationCommands.Copy" />
<MenuItem Name="mnDuplicate" Command="{x:Static classes:myCommands.Duplicate}"/>
On a side note RoutedUICommand has overloaded constructor that accepts InputGestureCollection where you can bind Ctrl+D when you command is created.
private static readonly RoutedUICommand _duplicateCmd =
new RoutedUICommand(
"Duplicate",
"Duplicate",
typeof(myCommands),
new InputGestureCollection(new InputGesture[] { new KeyGesture(Key.D, ModifierKeys.Control) }));
advantage is that you won't have to specify InputGestureText, as you don't have to ApplicationCommands.Copy, and because you use RoutedUICommand you also don't need to specify Header. If you do that both InputGestureText and Header will be taken by default from your RoutedUICommand and all you actually need to specify against MenuItem is a Command
I have a UserControl called ActionsTreeView I built using MVVM practices where I have an IPluginsProvider interface that populates the data in my UserControl. I want to be able to provide an object implementating this IContentProvider interface as a parameter to initialize my UserControl's ViewModel.
Here is my approach so far, which isn't working. I am wondering if I'm going down the right path? I declare a DependencyProperty in my user control which is visible to my mainWindow where I want to instantiate this UserControl. This code just attempts to pass the PluginsProvider object to my UserControl which needs it to build its ViewModel.
My PluginProvider DependencyProperty setter in my UserControl never gets hit because my My PropertyChanged handler is always null in MainWindow.xaml.cs I think I have the code right, but not sure I'm going down the right road and what I'm missing to make this connection?
ActionsTreeView.xaml.cs
public partial class ActionsTreeView: UserControl
{
public static readonly DependencyProperty PluginProviderProperty = DependencyProperty.Register("PluginProvider", typeof(Models.IPluginsProvider), typeof(ActionsTreeView), new FrameworkPropertyMetadata(null, OnPluginProviderChanged));
private ViewModels.ActionsTreeViewModel vm;
public ActionsTreeView()
{
//Wire-up our ViewModel with the data provider and bind it to DataContext for our user control
//This is a Mock-up until I figure out a way to get the real provider here
Models.IPluginProvider pluginSource = new Models.MockPluginProvider();
vm = new ViewModels.ActionsTreeViewModel(pluginSource );
this.DataContext = vm;
InitializeComponent();
}
private static void OnPluginProviderChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
((ActionsTreeView)source).PluginProvider = (Models.IPluginsProvider)e.NewValue;
}
public Models.IPluginsProvider PluginProvider
{
get
{
return (Models.IPluginsProvider)GetValue(PluginProviderProperty);
}
set
{
SetValue(PluginProviderProperty, value);
vm.SetPluginSource(PluginProvider);
}
}...
MainWindow.xaml.cs
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public MainWindow()
{
InitializeComponent();
this.ActionProvider = new Models.PluginsProvider(Library.Action.AvailableActions);
}
private Models.IPluginsProvider _actionProvider;
public Models.IPluginsProvider ActionProvider
{
get { return _actionProvider; }
set
{
_actionProvider = value;
OnPropertyChanged("ActionProvider");
}
}
protected void OnPropertyChanged(string property)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) //HANDLER IS ALWAYS NULL
{
handler(this, new PropertyChangedEventArgs(property));
}
}
}
Using my UserControl in MainWindow.xaml
<Grid>
<UserControls:ActionsTreeView PluginProvider="{Binding ActionProvider}" />
</Grid>
I don't think you can pass a parameter in the ctor in xaml.
If you create control in code behind you can pass the parameter in the ctor(Param param)
Not sure if this fits in the MVVM model but I use it a lot in regular code behind
Use a frame in the XAML for a place to put the UserControl
Seems like you are missing the binding source
<Grid>
<UserControls:ActionsTreeView PluginProvider="{Binding ActionProvider, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />
</Grid>
since your property ActionProvider is declared in MainWindow so during binding you are required to refer the same source unless you've set it as data context of the window
alternative to above you can also do the below if there is no other data context used in the MainWindow then you can use the original binding you have PluginProvider="{Binding ActionProvider}"
public MainWindow()
{
InitializeComponent();
this.ActionProvider = new Models.PluginsProvider(Library.Action.AvailableActions);
DataContext = this;
}
I've set the DataContext to this which will effectively resolve the value of ActionProvider in binding from the instance this
Extra
you may also choose to remove INotifyPropertyChanged from MainWindow as it is already DependencyObject and capable of property notification and declare a DependencyProperty for ActionProvider
eg
public Models.IPluginsProvider ActionProvider
{
get { return (Models.IPluginsProvider)GetValue(ActionProviderProperty); }
set { SetValue(ActionProviderProperty, value); }
}
// Using a DependencyProperty as the backing store for ActionProvider. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ActionProviderProperty =
DependencyProperty.Register("ActionProvider", typeof(Models.IPluginsProvider), typeof(MainWindow), new PropertyMetadata(null));
so you don't need to worry for the notification change manually, you might be required to use this if the above solution does not work for you otherwise it is good to have.
I have two UserControls (uc1 and uc2) loading into a third UserControl (shell). Shell has two properties, uc1 and uc2, of type UserControl1 and UserControl2, and each have a DependencyProperty registered to their own classes called IsDirty:
public static readonly DependencyProperty IsDirtyProperty = DependencyProperty.Register("IsDirty", typeof (bool), typeof (UserControl1));
public bool IsDirty
{
get { return (bool) GetValue(IsDirtyProperty); }
set { SetValue(IsDirtyProperty, value); }
}
(same code for UserControl2)
Shell has TextBlocks bound to the IsDirty properties:
<TextBlock Text="{Binding ElementName=shell, Path=Uc1.IsDirty}"/>
<TextBlock Text="{Binding ElementName=shell, Path=Uc2.IsDirty}"/>
When I change the values of IsDirty in uc1 and uc2, Shell never gets notified. What am I missing? UserControl is descendant of DependencyObject...
The same behavior occurs if I have regular properties notifying changes via INotifyPropertyChanged.
If I raise a routed event from uc1 and uc2, bubbling up to Shell, then I can catch the Dirty value and everything works, but I shouldn't have to do that, should I?
Thanks
Edit: The answer is to raise property changed event on the Uc1 and Uc2 properties or make them DPs.
I tried reproducing your problem using a simple setup, and it works fine for me. I'm not sure though if this setup is correct enough to replicate your situation. Anyway, I'm posting it just in case. It might be helpful:
XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
x:Name="shell"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Button Click="Button_Click">Click</Button>
<TextBlock Text="{Binding ElementName=shell, Path=Uc1.IsDirty}"/>
</StackPanel>
</Window>
Code-Behind:
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private MyUserControl uc1 = new MyUserControl();
public MyUserControl Uc1
{
get { return this.uc1; }
}
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.uc1.IsDirty = !this.uc1.IsDirty;
}
}
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
}
public bool IsDirty
{
get { return (bool)GetValue(IsDirtyProperty); }
set { SetValue(IsDirtyProperty, value); }
}
// Using a DependencyProperty as the backing store for IsDirty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsDirtyProperty =
DependencyProperty.Register("IsDirty", typeof(bool), typeof(UserControl), new UIPropertyMetadata(false));
}
}
Karmicpuppet's answer works well. However it didn't solve my problem because Shell is also of type UserControl. For it to work I needed to raise the property changed on Uc1 and Uc2. When I declared them as DependencyProperties all worked as expected. Duh!
I'm currently writing DataTemplate for my custom type lets say FootballPlayer. In this template I would like to put ie. Button and make that button when clicked call some of FootballPlayer functions eg. Run().
Is there any simple or complex, but clean way to achieve this kind of behaviour?
The DataTemplate I believe is aware of all the information about my object because DataType is set and clr-namespace is included.
<DataTemplate DataType="{x:Type my:FootballPlayer}">
</DataTemplate>
I suppose there is a clean way to achieve this. Can anyone tell me how?
//edit
The solution doesn't have to be clean. Now, after some investigation I'm just looking for any solution that can make a call to function / raise an event on the object being bound.
Yes, there is a clean way to do this. One aspect of using the Model-View-ViewModel pattern in WPF (not that you have to use this) is commanding. WPF Commanding reference
Here is a simplistic but clean and fairly type-safe framework class for exposing commands from your data source object:
using System;
using System.Windows.Input;
namespace MVVM
{
/// <summary>
/// Defines a command that can be bound to from XAML and redirects to a handler function.
/// </summary>
public class ViewModelCommand : ICommand
{
private Action _handler;
public ViewModelCommand(Action handler)
{
_handler = handler;
}
#region ICommand Members
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_handler();
}
#endregion
}
/// <summary>
/// Defines a command that can be bound to from XAML and redirects to a handler function.
/// </summary>
public class ViewModelCommand<T> : ICommand
where T : class
{
private Action<T> _handler;
public ViewModelCommand(Action<T> handler)
{
_handler = handler;
}
#region ICommand Members
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_handler(parameter as T);
}
#endregion
}
}
Your data source (e.g., FootballPlayer class) then exposes a command property as follows:
/// <summary>
/// Tell the player to run. This particular command takes a string as a parameter.
/// </summary>
public ICommand RunCommand
{
get { return new ViewModelCommand<string>(run); }
}
The implementation function, in the same FootballPlayer class, can then look like this:
/// <summary>
/// Tell the player to run. This particular command takes a string as a parameter.
/// </summary>
public void search(string destination)
{
System.Windows.MessageBox.Show(destination, "Running to destination...");
}
Finally, your XAML has the following databinding:
<Button Content="{Binding PlayerName}" FontSize="16" CommandParameter="{Binding Text, ElementName=txtDestination}" Command="{Binding RunCommand, Source={StaticResource ViewModelDataSource}}" />
(Since you're using a DataTemplate, the sources of the bindings would need to be adjusted; but that's the gist of it. I've used this with great success in a class project - it allowed a very clean separation between the logic and the UI.)
If you set up a generic handler for the event:
<Button Click="FootballPlayer_Run"/>
the DataContext of the e.OriginalSource will be the FootballPlayer object that is used for binding. You can then call Run on that object.
private void FootballPlayer_Run(object sender, RoutedEventArgs e)
{
FrameworkElement ele = e.OriginalSource as FrameworkElement;
if (ele != null)
{
FootballPlayer fp = ele.DataContext as FootballPlayer;
if (fp != null)
{
fp.Run();
}
}
e.Handled = true;
}
Is there any way to detect a change in the Text property of a TextBlock element using events?
(I'm trying to provide an animation for highlighting the TextBlocks whose Text property change within a DataGrid)
It's easier than that! Late answer, but much simpler.
// assume textBlock is your TextBlock
var dp = DependencyPropertyDescriptor.FromProperty(
TextBlock.TextProperty,
typeof(TextBlock));
dp.AddValueChanged(textBlock, (sender, args) =>
{
MessageBox.Show("text changed");
});
This is like the code from the link in bioskope's answer, but simplified. You need the TargetUpdated event and add NotifyOnTargetUpdated=True to the binding.
<TextBlock Text="{Binding YourTextProperty, NotifyOnTargetUpdated=True}"
TargetUpdated="YourTextEventHandler"/>
As far as I can understand there isn't any textchanged event in TextBlock. Looking at your requirement, I feel that re-templating a textbox will also not be a viable solution. From my preliminary searching around, this seems to be a possible solution.
<TextBlock x:Name="tbMessage" Text="{Binding Path=StatusBarText, NotifyOnTargetUpdated=True}">
<TextBlock.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:0″
To="1.0″ />
<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:2″
From="1.0″ To="0.0″ BeginTime="0:0:5″ />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
Bind the Text property to a DependencyProperty, which has an event trigger:
public static string GetTextBoxText(DependencyObject obj)
{
return (string)obj.GetValue(TextBoxTextProperty);
}
public static void SetTextBoxText(DependencyObject obj, string value)
{
obj.SetValue(TextBoxTextProperty, value);
}
public static readonly DependencyProperty TextBoxTextProperty =
DependencyProperty.RegisterAttached(
"TextBoxText",
typeof(string),
typeof(TextBlockToolTipBehavior),
new FrameworkPropertyMetadata(string.Empty, TextBoxTextChangedCallback)
);
private static void TextBoxTextChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextBlock textBlock = d as TextBlock;
HandleTextChange(textBlock);
}
In the XAML Bind to the TextBlock text Property:
<TextBlock
Text="{Binding SomeProperty, UpdateSourceTrigger=PropertyChanged}"
th:TextBlockBehavior.TextBoxText="{Binding Text,
RelativeSource={RelativeSource Self}}" />
Here is a similar example on MSDN using code-behind:
http://msdn.microsoft.com/en-us/library/system.windows.data.binding.targetupdated.aspx
Here's something you can use I picked up from Jerry Nixon and Daren May at the Microsoft Virtual Academy "Developing Universal Windows Apps with C# and XAML" and the code that contains the DependencyObject logic is here "(W8.1-WP8.1) UNIVERSAL APP FOR MVA".
namespace App1.Behaviors
{
// <summary>
/// Helper class that allows you to monitor a property corresponding to a dependency property
/// on some object for changes and have an event raised from
/// the instance of this helper that you can handle.
/// Usage: Construct an instance, passing in the object and the name of the normal .NET property that
/// wraps a DependencyProperty, then subscribe to the PropertyChanged event on this helper instance.
/// Your subscriber will be called whenever the source DependencyProperty changes.
/// </summary>
public class DependencyPropertyChangedHelper : DependencyObject
{
/// <summary>
/// Constructor for the helper.
/// </summary>
/// <param name="source">Source object that exposes the DependencyProperty you wish to monitor.</param>
/// <param name="propertyPath">The name of the property on that object that you want to monitor.</param>
public DependencyPropertyChangedHelper(DependencyObject source, string propertyPath)
{
// Set up a binding that flows changes from the source DependencyProperty through to a DP contained by this helper
Binding binding = new Binding
{
Source = source,
Path = new PropertyPath(propertyPath)
};
BindingOperations.SetBinding(this, HelperProperty, binding);
}
/// <summary>
/// Dependency property that is used to hook property change events when an internal binding causes its value to change.
/// This is only public because the DependencyProperty syntax requires it to be, do not use this property directly in your code.
/// </summary>
public static DependencyProperty HelperProperty =
DependencyProperty.Register("Helper", typeof(object), typeof(DependencyPropertyChangedHelper), new PropertyMetadata(null, OnPropertyChanged));
/// <summary>
/// Wrapper property for a helper DependencyProperty used by this class. Only public because the DependencyProperty syntax requires it.
/// DO NOT use this property directly.
/// </summary>
public object Helper
{
get { return (object)GetValue(HelperProperty); }
set { SetValue(HelperProperty, value); }
}
// When our dependency property gets set by the binding, trigger the property changed event that the user of this helper can subscribe to
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var helper = (DependencyPropertyChangedHelper)d;
helper.PropertyChanged(d, e);
}
/// <summary>
/// This event will be raised whenever the source object property changes, and carries along the before and after values
/// </summary>
public event EventHandler<DependencyPropertyChangedEventArgs> PropertyChanged = delegate { };
}
}
Usage XAML:
<TextBlock Grid.Row="0"
x:Name="WritingMenuTitle"
HorizontalAlignment="Left"
FontSize="32"
FontWeight="SemiBold"
Text="{Binding WritingMenu.Title}"
TextAlignment="Left"
TextWrapping="Wrap"/>
Usage xaml.cs:
Behaviors.DependencyPropertyChangedHelper helper = new Behaviors.DependencyPropertyChangedHelper(this.WritingMenuTitle, Models.CommonNames.TextBlockText);
helper.PropertyChanged += viewModel.OnSenarioTextBlockTextChangedEvent;
Usage viewmodel.cs:
public async void OnSenarioTextBlockTextChangedEvent(object sender, DependencyPropertyChangedEventArgs args)
{
StringBuilder sMsg = new StringBuilder();
try
{
Debug.WriteLine(String.Format(".....WritingMenuTitle : New ({0}), Old ({1})", args.NewValue, args.OldValue));
}
catch (Exception msg)
{
#region Exception
.....
#endregion
}
}