Is there any way that i can invoke a command and pass the SelectedItem as parameter to ViewModel when the selection change occurs?
XAML:
<telerik:GridViewComboBoxColumn ItemsSource="{Binding StatusList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValueMemberPath="StatusName" DisplayMemberPath="StatusName" DataMemberBinding="{Binding Shipped, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsVisible="{Binding IsExist, Mode=TwoWay}">
</telerik:GridViewComboBoxColumn>
I tried like adding Interation Triggers but i couldn't able to find the exact event to pass the SelectedItem as parameter,
<i:Interaction.Triggers>
<i:EventTrigger EventName="ContextMenuClosing">
<i:InvokeCommandAction Command="{Binding StatusDropdownCommand, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
ViewModel:
public ICommand StatusDropdownCommand { get { return new RelayCommand(StatusDropdown); } }
void StatusDropdown()
{
}
Kindly help.
Updated Code:
<telerik:GridViewComboBoxColumn ItemsSource="{Binding StatusList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValueMemberPath="StatusName" DisplayMemberPath="StatusName" DataMemberBinding="{Binding Shipped, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsVisible="{Binding IsExist, Mode=TwoWay}">
<i:Interaction.Triggers>
<Converter:RoutedEventTrigger RoutedEvent="Selector.SelectionChanged" >
<Converter:CustomCommandAction Command="{Binding SelectionChangedCommand}" />
</Converter:RoutedEventTrigger>
</i:Interaction.Triggers>
</telerik:GridViewComboBoxColumn>
Issue Occured:
Seems that subscription to Selector.SelectionChanged routed event should do the job.
<i:Interaction.Triggers>
<local:RoutedEventTrigger RoutedEvent="Selector.SelectionChanged">
<local:CustomCommandAction Command="{Binding SelectionChangedCommand}" />
</local:RoutedEventTrigger>
</i:Interaction.Triggers>
You need custom trigger to handle attached events:
public class RoutedEventTrigger : EventTriggerBase<DependencyObject>
{
RoutedEvent _routedEvent;
public RoutedEvent RoutedEvent
{
get { return _routedEvent; }
set { _routedEvent = value; }
}
public RoutedEventTrigger()
{
}
protected override void OnAttached()
{
Behavior behavior = base.AssociatedObject as Behavior;
FrameworkElement associatedElement = base.AssociatedObject as FrameworkElement;
if (behavior != null)
{
associatedElement = ((IAttachedObject)behavior).AssociatedObject as FrameworkElement;
}
if (associatedElement == null)
{
throw new ArgumentException("Routed Event trigger can only be associated to framework elements");
}
if (RoutedEvent != null)
{
associatedElement.AddHandler(RoutedEvent, new RoutedEventHandler(this.OnRoutedEvent));
}
}
void OnRoutedEvent(object sender, RoutedEventArgs args)
{
base.OnEvent(args);
}
protected override string GetEventName()
{
return RoutedEvent.Name;
}
}
Also you may use your own action for triggering your command:
public sealed class CustomCommandAction : TriggerAction<DependencyObject>
{
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(CustomCommandAction), null);
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
"Command", typeof(ICommand), typeof(CustomCommandAction), null);
public ICommand Command
{
get
{
return (ICommand)this.GetValue(CommandProperty);
}
set
{
this.SetValue(CommandProperty, value);
}
}
public object CommandParameter
{
get
{
return this.GetValue(CommandParameterProperty);
}
set
{
this.SetValue(CommandParameterProperty, value);
}
}
protected override void Invoke(object parameter)
{
if (this.AssociatedObject != null)
{
ICommand command = this.Command;
if (command != null)
{
if (this.CommandParameter != null)
{
if (command.CanExecute(this.CommandParameter))
{
command.Execute(this.CommandParameter);
}
}
else
{
if (command.CanExecute(parameter))
{
command.Execute(parameter);
}
}
}
}
}
}
Related
I am working on datagrid sample based on this one.
<dg:DataGrid x:Name="datagrid" ItemsSource="{Binding Teams}" SelectionEnabled="True" SelectedItem="{Binding SelectedTeam}" ActiveRowColor="Red"
RowHeight="70" HeaderHeight="50" BorderColor="#CCCCCC" HeaderBackground="#E0E6F8" Focused="Datagrid_Focused"
PullToRefreshCommand="{Binding RefreshCommand}" IsRefreshing="{Binding IsRefreshing}"
>
<x:Arguments>
<ListViewCachingStrategy>RetainElement</ListViewCachingStrategy>
</x:Arguments>
<dg:DataGrid.HeaderFontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Tablet>15</OnIdiom.Tablet>
<OnIdiom.Phone>12</OnIdiom.Phone>
</OnIdiom>
</dg:DataGrid.HeaderFontSize>
<dg:DataGrid.Columns>
<dg:DataGridColumn Title="Logo" PropertyName="Logo" Width="100" SortingEnabled="False">
<dg:DataGridColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" Aspect="AspectFit" HeightRequest="60" />
</DataTemplate>
</dg:DataGridColumn.CellTemplate>
</dg:DataGridColumn>
<dg:DataGridColumn Title="Team" PropertyName="Name" Width="2*">
<dg:DataGridColumn.CellTemplate>
<DataTemplate>
<Grid x:Name="gridtest" BindingContext="{Binding .}">
<Grid.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
</Grid.GestureRecognizers>
<Label x:Name="label1" Text="{Binding Name,Mode=TwoWay}" />
<Editor x:Name="editorTest" Completed="Editor_Completed" BindingContext="{Binding .}" Focused="Editor_Focused">
</Editor>
</Grid>
</DataTemplate>
</dg:DataGridColumn.CellTemplate>
</dg:DataGridColumn>
<dg:DataGridColumn Title="Win" PropertyName="Win" Width="0.95*"/>
<dg:DataGridColumn Title="Loose" PropertyName="Loose" Width="1*"/>
<dg:DataGridColumn PropertyName="Home">
<dg:DataGridColumn.FormattedTitle>
<FormattedString>
<Span Text="Home" ForegroundColor="Black" FontSize="13" FontAttributes="Bold"/>
<Span Text=" (win-loose)" ForegroundColor="#333333" FontSize="11" />
</FormattedString>
</dg:DataGridColumn.FormattedTitle>
</dg:DataGridColumn>
<dg:DataGridColumn Title="Percentage" PropertyName="Percentage" StringFormat="{}{0:0.00}" />
</dg:DataGrid.Columns>
</dg:DataGrid>
I have a Team model, the name what i entered in editor should be bind in that model property. In static cases it is working. How to acheive in dynamic case like,
I have add button . In add button only i am adding item to the collection Teams,
private void AddButton_Clicked(object sender, EventArgs e)
{
viewModel.Teams.Add(new Models.Team()
{
Win = 73,
Name = "",
Loose = 9,
Percentage = 0.89,
Conf = "46-6",
Div = "15-1",
Home = "39-2",
Road = "34-7",
Last10 = "8-2",
Streak = "W 4",
Logo = "gsw.png"
});
}
When editor is opened, datagrid selectedItem event is not working.and the text is not binding to the model property Name.
On clicking the Save Button, I need updated collection.
How to acheive this scenario?
Since you had used MVVM , you should handle all the logic in ViewModel. You could bind the Text of Editor in model.
<Editor Text="{Binding editorText,Mode=TwoWay}"... />
in model
define a new property
public class Team : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
string editorText;
public string EditorText
{
get
{
return editorText;
}
set
{
if(value!=null)
{
editorText = value;
OnPropertyChanged("EditorText");
}
}
}
//...
}
When editor is opened, datagrid selectedItem event is not working.
This maybe a issue of the plugin . You could set the SelectItem when the editor is focused .
Add the following class in your project
using System;
using Xamarin.Forms;
namespace xxx
{
public class BehaviorBase<T> : Behavior<T> where T : BindableObject
{
public T AssociatedObject { get; private set; }
protected override void OnAttachedTo (T bindable)
{
base.OnAttachedTo (bindable);
AssociatedObject = bindable;
if (bindable.BindingContext != null) {
BindingContext = bindable.BindingContext;
}
bindable.BindingContextChanged += OnBindingContextChanged;
}
protected override void OnDetachingFrom (T bindable)
{
base.OnDetachingFrom (bindable);
bindable.BindingContextChanged -= OnBindingContextChanged;
AssociatedObject = null;
}
void OnBindingContextChanged (object sender, EventArgs e)
{
OnBindingContextChanged ();
}
protected override void OnBindingContextChanged ()
{
base.OnBindingContextChanged ();
BindingContext = AssociatedObject.BindingContext;
}
}
}
using System;
using System.Reflection;
using System.Windows.Input;
using Xamarin.Forms;
namespace xxx
{
public class EventToCommandBehavior : BehaviorBase<View>
{
Delegate eventHandler;
public static readonly BindableProperty EventNameProperty = BindableProperty.Create ("EventName", typeof(string), typeof(EventToCommandBehavior), null, propertyChanged: OnEventNameChanged);
public static readonly BindableProperty CommandProperty = BindableProperty.Create ("Command", typeof(ICommand), typeof(EventToCommandBehavior), null);
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create ("CommandParameter", typeof(object), typeof(EventToCommandBehavior), null);
public static readonly BindableProperty InputConverterProperty = BindableProperty.Create ("Converter", typeof(IValueConverter), typeof(EventToCommandBehavior), null);
public string EventName {
get { return (string)GetValue (EventNameProperty); }
set { SetValue (EventNameProperty, value); }
}
public ICommand Command {
get { return (ICommand)GetValue (CommandProperty); }
set { SetValue (CommandProperty, value); }
}
public object CommandParameter {
get { return GetValue (CommandParameterProperty); }
set { SetValue (CommandParameterProperty, value); }
}
public IValueConverter Converter {
get { return (IValueConverter)GetValue (InputConverterProperty); }
set { SetValue (InputConverterProperty, value); }
}
protected override void OnAttachedTo (View bindable)
{
base.OnAttachedTo (bindable);
RegisterEvent (EventName);
}
protected override void OnDetachingFrom (View bindable)
{
DeregisterEvent (EventName);
base.OnDetachingFrom (bindable);
}
void RegisterEvent (string name)
{
if (string.IsNullOrWhiteSpace (name)) {
return;
}
EventInfo eventInfo = AssociatedObject.GetType ().GetRuntimeEvent (name);
if (eventInfo == null) {
throw new ArgumentException (string.Format ("EventToCommandBehavior: Can't register the '{0}' event.", EventName));
}
MethodInfo methodInfo = typeof(EventToCommandBehavior).GetTypeInfo ().GetDeclaredMethod ("OnEvent");
eventHandler = methodInfo.CreateDelegate (eventInfo.EventHandlerType, this);
eventInfo.AddEventHandler (AssociatedObject, eventHandler);
}
void DeregisterEvent (string name)
{
if (string.IsNullOrWhiteSpace (name)) {
return;
}
if (eventHandler == null) {
return;
}
EventInfo eventInfo = AssociatedObject.GetType ().GetRuntimeEvent (name);
if (eventInfo == null) {
throw new ArgumentException (string.Format ("EventToCommandBehavior: Can't de-register the '{0}' event.", EventName));
}
eventInfo.RemoveEventHandler (AssociatedObject, eventHandler);
eventHandler = null;
}
void OnEvent (object sender, object eventArgs)
{
if (Command == null) {
return;
}
object resolvedParameter;
if (CommandParameter != null) {
resolvedParameter = CommandParameter;
} else if (Converter != null) {
resolvedParameter = Converter.Convert (eventArgs, typeof(object), null, null);
} else {
resolvedParameter = eventArgs;
}
if (Command.CanExecute (resolvedParameter)) {
Command.Execute (resolvedParameter);
}
}
static void OnEventNameChanged (BindableObject bindable, object oldValue, object newValue)
{
var behavior = (EventToCommandBehavior)bindable;
if (behavior.AssociatedObject == null) {
return;
}
string oldEventName = (string)oldValue;
string newEventName = (string)newValue;
behavior.DeregisterEvent (oldEventName);
behavior.RegisterEvent (newEventName);
}
}
}
in xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:xxx"
mc:Ignorable="d"
x:Name="page" // set name here
x:Class="xxx.MainPage">
<Editor.Behaviors>
<local:EventToCommandBehavior EventName="Focused" Command="{Binding Source={x:Reference page},Path=BindingContext.xxxCommand}" CommandParameter="{Binding }" />
</Editor.Behaviors>
in ViewModel
xxxCommand = new Command((model)=>{
var item = model as Team;
SelectedTeam = item;
// ...
});
I have a ContextMenu that suppose to set value on its parent TextBox.
The textbox cannot have a name (by requirement), so I am setting it as CommandTarget
<TextBox Text="{Binding TextBoxOne, UpdateSourceTrigger=LostFocus}">
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Set to 35"
Command="{Binding SetAmountCommand}"
CommandParameter="35"
CommandTarget="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBox}}}" />
<MenuItem Header="Set to 50"
Command="{Binding SetAmountCommand}"
CommandParameter="50"
CommandTarget="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBox}}}" />
</ContextMenu>
</TextBox.ContextMenu>
How to access the TextBox.Text from inside the Command ?
ViewModel
public class MainVm : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string TextBoxOne { get; set; } = "One";
private ICommand _setAmountCommand;
public ICommand SetAmountCommand
{
get
{
return _setAmountCommand ?? (_setAmountCommand = new CommandParameterHandler((o) =>
{
object param = o;
double amount = (double)o;
//MyParentTextBox.Text = amount; //What to put here ? (Cannot be TextBoxOne = amount, need to route from View)
}, true));
}
}
}
Generic CommandParameterHandler
public class CommandParameterHandler : ICommand
{
private Action<object> _action;
private bool _canExecute;
public CommandParameterHandler(Action<object> action, bool canExecute)
{
_action = action;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_action(parameter);
}
}
You can only pass one CommandParameter to the command. If you want to pass is something in addition to the actual value, you could create a custom composite type that carries more than one value:
public class CompositeParameter : Freezable
{
protected override Freezable CreateInstanceCore()
{
return this;
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(nameof(Value),
typeof(string), typeof(CompositeParameter));
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ControlProperty = DependencyProperty.Register(nameof(Control),
typeof(FrameworkElement), typeof(CompositeParameter));
public FrameworkElement Control
{
get { return (FrameworkElement)GetValue(ControlProperty); }
set { SetValue(ControlProperty, value); }
}
}
View Model:
public ICommand SetAmountCommand
{
get
{
return _setAmountCommand ?? (_setAmountCommand = new CommandParameterHandler((o) =>
{
CompositeParameter param = o as CompositeParameter;
if (param != null)
{
double amount = Convert.ToDouble(param.Value);
//...
TextBox textBox = param.Control as TextBox;
if (textBox != null)
textBox.Text = param.Value;
}
}, true));
}
}
View:
<TextBox Text="{Binding TextBoxOne, UpdateSourceTrigger=LostFocus}">
<TextBox.ContextMenu>
<ContextMenu>
<ContextMenu.Resources>
<local:CompositeParameter x:Key="paramA"
Value="35"
Control="{Binding PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
<local:CompositeParameter x:Key="paramB"
Value="50"
Control="{Binding PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
</ContextMenu.Resources>
<MenuItem Header="Set to 35"
Command="{Binding SetAmountCommand}"
CommandParameter="{StaticResource paramA}" />
<MenuItem Header="Set to 50"
Command="{Binding SetAmountCommand}"
CommandParameter="{StaticResource paramB}" />
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
After 2 days searching for answer, I came across this RoutedCommand tutorial. Yes, you can access CommandTarget from Command, but it has to be a static RoutedCommand. This approach fits the need as SetAmountCommand is shared by multiple MenuItem.
XAML
<Window x:Class="WpfCommandTargetDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfCommandTargetDemo">
<Window.CommandBindings>
<CommandBinding CanExecute="SetAmountCommand_CanExecute"
Command="{x:Static local:CustomRoutedCommand.SetAmountCommand}"
Executed="SetAmountCommand_Executed" />
</Window.CommandBindings>
<StackPanel>
<TextBox Text="{Binding TextBoxOne, UpdateSourceTrigger=LostFocus}">
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Set to 35"
Command="{x:Static local:CustomRoutedCommand.SetAmountCommand}"
CommandParameter="35"
CommandTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget}" />
<MenuItem Header="Set to 50"
Command="{x:Static local:CustomRoutedCommand.SetAmountCommand}"
CommandParameter="50"
CommandTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget}" />
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
</StackPanel>
</Window>
CodeBehind
public partial class MainWindow : Window
{
private readonly MainVm _mainVm;
public MainWindow()
{
InitializeComponent();
_mainVm = new MainVm();
DataContext = _mainVm;
}
void SetAmountCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
void SetAmountCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
object param = e.Parameter; //CommandParameter
TextBox textbox = e.OriginalSource as TextBox; //CommandTarget
if (textbox != null)
{
textbox.Text = param.ToString();
}
}
}
RoutedCommand has to be static, because it is statically bound to XAML element.
public static class CustomRoutedCommand
{
public static readonly RoutedCommand SetAmountCommand = new RoutedCommand();
}
For completeness, I cannot have the Command on my ViewModel. SetAmountCommand property is removed.
public class MainVm : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string TextBoxOne { get; set; } = "One";
}
I'm doing my first app using MVVM. I have in "View" declared Datagrid. Code XAML below:
<DataGridTemplateColumn Header="delete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type> UserControl},Mode=FindAncestor}, Path=DataContext.ClickCommand}"> Content="X" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>>
</DataGrid>
In my ViewModel class I can run function that I want after click button "delete" by part of code:
public ICommand ClickCommand => _clickCommand ?? (_clickCommand = new CommandHandler(Delete, _canExecute));
public void Delete()
{
// DataTable.Rows.RemoveAt();
}
I have problem because I can't get index of selectet row. Source of data in datagrid is dataTable.
Do you have any ideas how to do this?
I've tried something with passing parameter with command of button but I coudn't make it works.
Xmal code
<Button Command="{Binding Path=DataContext.ViewCommand,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding Id}" Content="X" Background="Chocolate"/>
Codebehind code
public RelayCommand DeleteCommand
{
get
{
return new RelayCommand(p => Delete(p));
}
}
public void Delete(string id)
{
// DataTable.Rows.RemoveAt();
}
This is example you can pass whatever you want in that cmd parameter.
Relay cmd
public class RelayCommand : ICommand
{
private Action<object> action;
private Func<bool> canFuncPerform;
public event EventHandler CanExecuteChanged;
public RelayCommand(Action<object> executeMethod)
{
action = executeMethod;
}
public RelayCommand(Action<object> executeMethod, Func<bool> canExecuteMethod)
{
action = executeMethod;
canFuncPerform = canExecuteMethod;
}
public void RaiseCanExecuteChanged()
{
CanExecuteChanged(this, EventArgs.Empty);
}
public bool CanExecute(object parameter)
{
if (canFuncPerform != null)
{
return canFuncPerform();
}
if (action != null)
{
return true;
}
return false;
}
public void Execute(object parameter)
{
if (action != null)
{
action(parameter);
}
}
}
You shouldn't rely on the selected item. Instead pass the current row item as CommandParameter:
<DataGridTemplateColumn Header="delete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},Mode=FindAncestor}, Path=DataContext.ClickCommand}"
CommandParameter="{Binding}"
Content="X" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Then of course, use an ICommand implementation that is not discarding the command parameter and use it to identify the row to be deleted.
My issue is that I wish to press enter after typing a text in a textbox. When I do, I want this to trigger a specific property in my viewmodel which is defined in my PCL (this can not be changed).
I have seen some examples that almost do the similar thing but they only do standard actions such as clear text in textbox or tab to next control and so on. I want this one to interact with a property of my choice.
HeaderView.xaml
<views:MvxWpfView
xmlns:views="clr-namespace:Cirrious.MvvmCross.Wpf.Views;assembly=Cirrious.MvvmCross.Wpf"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:UI="clr-namespace:ProductCatalog.UserInterface.WPF.Bootstrap"
x:Class="ProductCatalog.UserInterface.WPF.Views.HeaderView">
<Grid Height="70" Background="#005287">
<TextBox DataContext="{Binding SearchText}" UI:TextBoxExtension.EnterKey="Search"
Width="120" Height="35" Padding="8" Margin="10" HorizontalAlignment="Right" >
</TextBox>
</Grid></views:MvxWpfView>
HeaderView.xaml.cs
public partial class HeaderView : MvxWpfView
{
public new HeaderViewModel ViewModel
{
get { return (HeaderViewModel) base.ViewModel; }
set { base.ViewModel = value; }
}
public HeaderView()
{
InitializeComponent();
}
}
HeaderViewModel.cs (In PCL)
public class HeaderViewModel : MvxViewModel
{
private string _searchText;
public string SearchText
{
get { return _searchText; }
set
{
_searchText = value;
RaisePropertyChanged(() => SearchText);
}
}
public ICommand Search
{
get
{
return new MvxCommand(() =>
{
SearchItems = new List<string> { "Hey", "Hello", "Hola" };
});
}
}
private IList<string> _searchItems;
public IList<string> SearchItems
{
get { return _searchItems; }
set { _searchItems = value; RaisePropertyChanged(() => SearchItems); }
}
}
TextBoxExtension.cs
public static class TextBoxExtension
{
public static ICommand GetEnterKey(DependencyObject obj)
{
return (ICommand)obj.GetValue(EnterKey);
}
public static void SetEnterKey(DependencyObject obj, ICommand value)
{
obj.SetValue(EnterKey, value);
}
public static readonly DependencyProperty EnterKey =
DependencyProperty.RegisterAttached("EnterKey", typeof(ICommand), typeof(TextBoxExtension), new UIPropertyMetadata(EnterKeyPropertyChanged));
static void EnterKeyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement element = d as UIElement;
if (element == null)
{
return;
}
element.KeyDown += Keydown;
}
static void Keydown(object sender, KeyEventArgs e)
{
if (!e.Key.Equals(Key.Enter))
{
return;
}
UIElement element = sender as UIElement;
if (element != null)
{
ICommand command = GetEnterKey(element);
command.Execute(null);
}
}
}
Here is what happens, when I use the parameter Search into my UI:TextBoxEntension.EnterKey the event is triggered like I want it to, but it can't find my Search property in my ViewModel. When I try using {Binding Search} then the event is never triggered. How can I get the event to trigger my Search property in my ViewModel.
Many thanks
Have you tried using InputBindings:
<TextBox>
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding DoSomething}"/>
</TextBox.InputBindings>
</TextBox>
And in your ViewModel:
DoSomething = new RelayCommand(() => DoSomething(), () => true);
First, you should define your Attached property as
public static readonly DependencyProperty EnterKeyProperty =
DependencyProperty.RegisterAttached("EnterKey", typeof(ICommand), typeof(TextBoxExtension), new UIPropertyMetadata(EnterKeyPropertyChanged));
public static ICommand GetEnterKey(DependencyObject obj)
{
return (ICommand)obj.GetValue(EnterKeyProperty);
}
public static void SetEnterKey(DependencyObject obj, ICommand value)
{
obj.SetValue(EnterKeyProperty, value);
}
This is to differentiate between attached property and CLR property.
Second binding should happen as
<TextBox DataContext="{Binding SearchText}" UI:TextBoxExtension.EnterKey="{Binding Search}"
Width="120" Height="35" Padding="8" Margin="10" HorizontalAlignment="Right" >
</TextBox>
This is a silverlight application, Im using asynchronous filtering for an autocompletebox, the problem is that so far i fail to bind FilterAsyncCommand property from the behaviour with the corresponding ViewModel property.
Following is the xaml declaration for the control in the View:
<controls:ChildWindow x:Class="MyApp.Views.View1"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:fw ="clr-namespace:NetBoxSys.Views"
...
>
<sdk:AutoCompleteBox
MinimumPrefixLength="3" MinimumPopulateDelay="150"
SelectedItem="{Binding Path=...}" ItemsSource="{Binding Path=...}" >
<i:Interaction.Behaviors>
<fw:FilterAsyncBehavior FilterAsyncCommand="{Binding Path=FilterAsyncCommand}" />
</i:Interaction.Behaviors>
</sdk:AutoCompleteBox>
</controls:ChildWindow>
...ViewModel code:
private ICommand filterAsyncCommand;
public ICommand FilterAsyncCommand {
get { return filterAsyncCommand; }
set {
filterAsyncCommand = value;
this.OnPropertyChanged( "FilterAsyncCommand" );
}
}
...and this is how im loading the View
var view = new View1();
view.DataContext = new ViewModel1();
view.Show(); //Modal
I have tried this syntax too but does not work either:
<fw:FilterAsyncBehavior FilterAsyncCommand="{Binding FilterAsyncCommand}" />
Need advice for this kind of binding.
UPDATE:
Behaviour code:
public class FilterAsyncBehavior : Behavior<AutoCompleteBox>
{
public ICommand FilterAsyncCommand
{
get
{
return (ICommand)GetValue(FilterAsyncCommandProperty);
}
set
{
SetValue(FilterAsyncCommandProperty, value);
}
}
public static readonly DependencyProperty FilterAsyncCommandProperty = DependencyProperty.Register("FilterAsyncCommand",
typeof(ICommand),
typeof(FilterAsyncBehavior),
new PropertyMetadata(null));
protected override void OnAttached()
{
base.OnAttached();
// handle the populating event of the associated auto complete box
AssociatedObject.Populating += AssociatedObject_Populating;
}
protected override void OnDetaching()
{
// detach the event handler
AssociatedObject.Populating -= AssociatedObject_Populating;
base.OnDetaching();
}
private void AssociatedObject_Populating(object sender, PopulatingEventArgs e)
{
// get the command
ICommand filterCommand = FilterAsyncCommand;
if (filterCommand != null)
{
// create the parameters for the command
var parameters = new FilterAsyncParameters(AssociatedObject.PopulateComplete, e.Parameter);
// execute command
filterCommand.Execute(parameters);
// cancel the population of the auto complete box
e.Cancel = true;
}
}
}
ViewModel code:
public class ViewModel1 : ViewModel, IViewModel {
public ViewModel1() {
//Initializing the command in constructor
FilterAsyncCommand = new DelegateCommand<FilterAsyncParameters>( ExecuteFilterAsync );
}
private void ExecuteFilterAsync( FilterAsyncParameters args ) {
....
}
private ICommand filterAsyncCommand;
public ICommand FilterAsyncCommand {
get { return filterAsyncCommand; }
set {
filterAsyncCommand = value;
this.OnPropertyChanged( "FilterAsyncCommand" );
}
}
}
I find the solution for my problem. I give up with passing the ViewModel at runtime, so i changed to the following implementation of the MVVM pattern
<controls:ChildWindow x:Class="MyApp.Views.View1"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:fw ="clr-namespace:NetBoxSys.Views"
...
>
<controls:ChildWindow.Resources>
<viewModel:ViewModelRecepcionOCViewModelLocator x:Key="viewModelLocator"/>
</controls:ChildWindow.Resources>
<controls:ChildWindow.DataContext>
<Binding Source="{StaticResource viewModelLocator}" Path="ViewModel" />
</controls:ChildWindow.DataContext>
<sdk:AutoCompleteBox HorizontalAlignment="Stretch" Margin="2" x:Name="remitente_Autocomplete" VerticalAlignment="Center" Grid.Column="1" Grid.Row="4"
MinimumPrefixLength="3" MinimumPopulateDelay="150" Padding="0" Height="23" TabIndex="1" Text="{Binding Path=NombreRemitente,Mode=TwoWay}"
SelectedItem="{Binding Path=SelectedRemitente,Mode=TwoWay}" ItemsSource="{Binding Path=Remitentes}" >
<i:Interaction.Behaviors>
<fw:FilterAsyncBehavior FilterAsyncCommand="{Binding Source={StaticResource viewModelLocator},Path=ViewModel.FilterAsyncCommand}" />
</i:Interaction.Behaviors>
</sdk:AutoCompleteBox>
</controls:ChildWindow>
The important part here is the binding definition for FilterAsyncCommand.
And RecepcionOCViewModelLocator is defined like this
public class RecepcionOCViewModelLocator : ViewModelLocatorBase<IRecepcionOCViewModel>
{
public IRecepcionOCViewModel ViewModel{get;set;}
}