I am trying to bind a click on button event in WPF to a command defined in a View Model, here is how I am doing that for now :
In the xaml code :
<Grid>
<Button Content="Module A" Background="Green" FontWeight="Bold">
<i:Interaction.Triggers>
<i:EventTrigger EventName="click">
<i:InvokeCommandAction Command="{Binding ChargeModuleDCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
and in the ViewModel class :
class ModuleAViewModel
{
public DelegateCommand<object> ChargeModuleDCommand { get; set; }
public ModuleAViewModel()
{
ChargeModuleDCommand = new DelegateCommand<object>(LaunchDModule);
}
private void LaunchDModule(object parm)
{
Console.WriteLine("I am in the function");
}
}
but it does not work. I've tried to do it as specified in this question : How to trigger ViewModel command for a specific button events
but it does not work either.
Is there any way that I can make it work ?
<Button
Command="{Binding ChargeModuleDCommand}"
Content="Module A"
Background="Green"
FontWeight="Bold"
/>
If ModuleAViewModel is the Button's DataContext, that should work.
Related
So I use the Autosuggestionbox of the ModernWPF Toolkit and was trying to get to handle the "QuerySubmitted" event from it via the MVVM way. Here's what I have
<ui:AutoSuggestBox x:Name="TextBox"
Text="{Binding TextBoxID, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
ItemsSource="{Binding Path= TextBoxCollection}"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="QuerySubmitted" >
<i:InvokeCommandAction Command ="{Binding QuerySubmit}" CommandParameter="{Binding ElementName=TextBox}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ui:AutoSuggestBox>
Heres my ViewModel
public ICommand textBox
public ICommand QuerySubmit
{
get
{
if(textBox== null)
{
///Do some stuff when event fires
}
return textBox;
}
}
and for the namespace of i its
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
But it doesn't seem to work so far. Any guesses on why and how to fix it?
To add, I tried switching it to a different event (MouseDoubleClick) and it works, so I think it's about the event of the querysubmit itself.
How do i trigger the SourceUpdate of my wpf listview?
This is my xaml
<ListView DataContext="{StaticResource vmInstance}" SelectedItem="{Binding selectedItem}" BorderBrush="Beige" BorderThickness="1" ItemsSource="{Binding lstExternal}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged" >
<cmd:EventToCommand Command="{Binding Path=ItemChanged}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="SourceUpdated">
<cmd:EventToCommand Command="{Binding Path=SourceUpdated}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ListView.View >
</ListView.View>
</ListView>
ViewModel:
public static RelayCommand SourceUpdated { get; set; }
//on initialize
SourceUpdated = new RelayCommand(SourceUpdateEvent);
public void SourceUpdateEvent()
{
Console.WriteLine("Updated");
}
Why does SourceUpdateEvent doesn't run?
Am I using the wrong event?
Thank you
want to execute a method when the itemsource of the listview is changed or updated
Then bind the ItemsSource property of the ListView to a source property of your view model
<ListView ItemsSource="{Binding YourCollection}" ...>
...and invoke your command in the setter of this source property:
private ObservableCollection<YourType> _sourceCollection;
public ObservableCollection<YourType> SourceCollection
{
get { return _sourceCollection; }
set
{
_sourceCollection = value;
RaisePropertyChanged();
SourceUpdated.Execute(null);
}
}
This is the (only) correct way to solve this using the MVVM pattern.
I have a Textbox and for that textbox I have attached a keydown event. Everything is working fine but I just noticed that when i'm pressing the 'Backspace' and 'Delete' Key, the binding command is not being called.
My View xaml file :-
<TextBox x:Name="textBox" Width="500" Text="{Binding TextBoxText, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cmd:EventToCommand Command="{BindingPath=TextBoxKeyDownEvent}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
My ViewModel cs file :-
//TextBox Key Down Event Handler
private DelegateCommand _textBoxKeyDownEvent;
public ICommand TextBoxKeyDownEvent
{
get
{
if (_textBoxKeyDownEvent == null)
{
_textBoxKeyDownEvent = new DelegateCommand(TextBoxKeyDownEventHandler);
}
return _textBoxKeyDownEvent;
}
set { }
}
Can somebody give me some suggestion
EDIT:
You have to use PreviewKeyDown the it works. KeyDown is not fired on Space and Delete. If you ignore MVVM and put the handler of KeyDown in codebehind it will also fail.
How about binding the Text-Property to a string in you viewmodel?
I build a fast, simple example of my idea.
Result
Text from the TextBox on the left side is simply populated to the Textblock on the right side.
View
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding TextBoxValue, UpdateSourceTrigger=PropertyChanged}" Width="250"/>
<StackPanel Orientation="Horizontal">
<TextBlock>"</TextBlock>
<TextBlock Text="{Binding TextBoxValue, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock>"</TextBlock>
</StackPanel>
</StackPanel>
</Window>
ViewModel
public class MainWindowViewModel : INotifyPropertyChanged
{
private string textBoxValue;
public string TextBoxValue
{
get { return textBoxValue; }
set
{
textBoxValue = value;
OnTextBoxValueChanged();
RaisePropertyChanged();
}
}
void OnTextBoxValueChanged()
{
// you logic here, if needed.
}
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
you most use PreviewKeyDown event.
Like this:
<EventSetter Event="PreviewKeyDown" Handler="TextBox_PreviewKeyDown"/>
Edit: You are correct - the default behavior is not executed. You should use ec8ors solution, which is much better anyway:
<TextBox x:Name="textBox" Width="500" Text="{Binding TextBoxText, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewKeyDown">
<i:InvokeCommandAction Command="{Binding TextBoxKeyDownEvent, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
Original:
You can use InputBindings to call your command when "special" keys have been pressed:
<TextBox x:Name="textBox" Width="500" Text="{Binding TextBoxText, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cmd:EventToCommand Command="{BindingPath=TextBoxKeyDownEvent}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBox.InputBindings>
<KeyBinding Command="{Binding TextBoxKeyDownEvent}" Key="Delete" />
<KeyBinding Command="{Binding TextBoxKeyDownEvent}" Key="Back" />
</TextBox.InputBindings>
</TextBox>
I've got this goto functionality in my DataGrid. This functionality I would like to keep out of my ViewModel and out of code-behind, so the following attachment could be perfect, however...
The user enters a line(item) number then when the user clicks the GotoButton it brings the item into view.
<Grid>
<TextBox x:Name="GotoTextbox" Text="{Binding GotoLineNumber, UpdateSourceTrigger=PropertyChanged}" />
<Button Name="GotoButton" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<helpers:TargetedTriggerActionGotoButton TargetObject="{Binding ElementName=GenericDataGrid}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
Here is the TargetedTriggerAction class.
public class TargetedTriggerActionGotoButton : TargetedTriggerAction<DataGrid>
{
protected override void Invoke(object parameter)
{
this.Target.SelectedGridItem = GotoLineNumber - 1;
this.Target.SelectedGridIndex = GotoLineNumber.GetValueOrDefault() - 1;
}
}
I would like to somehow pass the text in from GotoTextbox, is there some binding I can do?. How could I achieve this?
As we spoke in comments
to allow parameters to be passed in, one should implement additional property in your TargetedTriggerAction
public class TargetedTriggerActionGotoButton : TargetedTriggerAction<DataGrid>
{
protected override void Invoke()
{
this.Target.SelectedGridItem = GotoLineNumber - 1;
this.Target.SelectedGridIndex = GotoLineNumber.GetValueOrDefault() - 1;
}
//property used as parameter
public object Parameter {get;set;}
}
and then in your xaml
<Grid>
<TextBox x:Name="GotoTextbox" Text="{Binding GotoLineNumber, UpdateSourceTrigger=PropertyChanged}" />
<Button Name="GotoButton" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<helpers:TargetedTriggerActionGotoButton TargetObject="{Binding ElementName=GenericDataGrid}" Parameter="{Binding ElementName="/*desiredName*/",Path="/*neededValue*/"}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
I am trying to implement modal dialog in the WPF Prism Desktop application.
From Prism guidance I can see that proper way should be using Interaction:
<i:Interaction.Triggers>
<prism:InteractionRequestTrigger
SourceObject="{Binding ConfirmCancelInteractionRequest}">
<prism:PopupChildWindowAction
ContentTemplate="{StaticResource ConfirmWindowTemplate}"/>
</prism:InteractionRequestTrigger>
</i:Interaction.Triggers>
But PopupChildWindowAction is not available in the Microsoft.Practices.Prism.Interactivity.DLL library for Desktop, only Silverlight?
I could google for many different implementations of the Modal Dialog in WPF (Prism), but just wondering why this feature is missing from Prism Desktop DLL and is available in Silverlight DLL?
I could use Interaction Service but Interaction Request is suggested as more appropriate approach for MVVM application.
That's true it only exists in the Silverlight prism library ,
What you can do is create your own .
CS :
public class OpenPopupWindowAction : TriggerAction<FrameworkElement>
{
protected override void Invoke(object parameter)
{
var popup = (ChildWindow)ServiceLocator.Current.GetInstance<IPopupDialogWindow>();
popup.Owner = PlacementTarget ?? (Window)ServiceLocator.Current.GetInstance<IShell>();
popup.DialogResultCommand = PopupDailogResultCommand;
popup.Show();
}
public Window PlacementTarget
{
get { return (Window)GetValue(PlacementTargetProperty); }
set { SetValue(PlacementTargetProperty, value); }
}
public static readonly DependencyProperty PlacementTargetProperty =
DependencyProperty.Register("PlacementTarget", typeof(Window), typeof(OpenPopupWindowAction), new PropertyMetadata(null));
public ICommand PopupDailogResultCommand
{
get { return (ICommand)GetValue(PopupDailogResultCommandProperty); }
set { SetValue(PopupDailogResultCommandProperty, value); }
}
public static readonly DependencyProperty PopupDailogResultCommandProperty =
DependencyProperty.Register("PopupDailogResultCommand", typeof(ICommand), typeof(OpenPopupWindowAction), new PropertyMetadata(null));
}
XAML :
<i:EventTrigger SourceObject="{Binding}" EventName="NavigatedFrom">
<popup:OpenPopupWindowAction PopupDailogResultCommand="{Binding OnNavigationConfirmed}"/>
</i:EventTrigger>
And if you need here is the Code for the DialogWindow it self .
cs:
public partial class ChildWindow : Window, IPopupDialogWindow
{
public ChildWindow()
{
InitializeComponent();
DataContext = this;
}
public new PopupDialogResult DialogResult
{
get;
set;
}
public System.Windows.Input.ICommand DialogResultCommand
{
get;
set;
}
}
xaml :
<Window x:Class="Utils.ActionPopupWindow.ChildWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="400" WindowStartupLocation="CenterOwner"
xmlns:popup="clr-namespace:Utils.ActionPopupWindow"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
x:Name="popUpWindow"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="30">
This is a child window <LineBreak/> launched from the <LineBreak/>main window
</TextBlock>
<StackPanel Grid.Row="1" Background="#FFA6A6A6">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Ok"
MinWidth="100"
Command="{Binding DialogResultCommand}"
CommandParameter="{x:Static popup:PopupDialogResult.OK}"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction MethodName="Close" TargetObject="{Binding ElementName=popUpWindow}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button Content="Cancel"
MinWidth="100"
Command="{Binding DialogResultCommand}"
CommandParameter="{x:Static popup:PopupDialogResult.Cancel}"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction MethodName="Close" TargetObject="{Binding ElementName=popUpWindow}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
</StackPanel>
</Grid>
Here is an example made in WPF:
http://blogs.southworks.net/dcherubini/2012/05/24/popupwindowaction-using-custom-views-instead-of-windows-in-wpf-and-prism/