OnmouseOver on hyperlink showing image in mvvm mvvm light wpf - wpf

I want to use hyperlink in wpf xaml. Whenever mouse comes over the hyperlink, it should show image related to that hyperlink and when mouse gets away the image should disappear. How to go about this using bindings. I am using mvvm light.
Kindly Suggest.
Thanks

The basic framework of what you will need, if you want to accomplish this in an MVVM style is...
You will need to start by setting up a Behavior to Command the Hyperlinks MouseEnter MouseLeave events.
<Hyperlink NavigateUri="Uri">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<Command:EventToCommand Command="HoverCommand" PassEventArgs="True" />
</i:EventTrigger>
<i:Interaction.Triggers>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeave">
<Command:EventToCommand Command="HoverCommand" PassEventArgs="True" />
</i:EventTrigger>
<i:Interaction.Triggers>
Link text.
</Hyperlink>
Now setup a control that will hover when the its DataContext is not null
Use the command to set the controls DataContext on hover to the Uri of the image, on Leave set the datacontext to null.

Related

Have a DataGrid Appear under a textbox

What I want is for a datagrid to appear under a textbox when the user starts. Once focus is lost from the textbox the datagrid disappears. I am having a hard time format it so does not screw up the rest of the windows formatting.
Before you ask about using a list box, I need multiple columns and the user should be able to reorder the list.
One more idea. Bind the visibility of the DataGrid to a property from your ViewModel. Initially you can set the Visibility to Visible.
Next you can use the Interactivity on the textbox's LostFocus event to change the Visibility to Hidden/Collapsed.
The following is an example
<TextBox Text="Test">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LostFocus">
<i:InvokeCommandAction Command="{Binding DataGridVisibilityCommand}" CommandParameter="Collapsed"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
<DataGrid Visibility="{Binding DataGridVisibility}"/>
add the namespace
xmlns:i="http://schemas.microsoft.com/expression/2010/interactions"
to your window/usercontrol and add the necessary dll references.

How to connect ICommand and VisualStateManager together

How can I let my controls on the window know that their states should be changed. I have to use ICommand and since controls can have different states (Enable/Disable , Checked/Unchecked ...) have to handle them with VisualStateManager.
This could be a possible scenario:
When Record button is clicked(Checked) the other controls in the window should be uncheckable and once the recording is finished they should be checkable and many other scenarios.
Any help would be really appreciated.
Thanks.
You can achieve it with the help of interaction and interactivity. See if it helps.
Add below references in your XAML
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
You can bind your ICommand and provide a VisualState name that will be applied when Button is clicked
<Button>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding YourICommand}" />
<ei:GoToStateAction StateName="YourVisualStateName" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
If you don't have Blend 4, you can install the Blend 4 SDK to get the current behavior assemblies.

Attaching Command to ScrollViewer.ScrollChanged of ListView

The WPF ListView class can be set up to automatically handle scrolling without an external ScrollViewer and it's possible to register an event handler for the control's internal scrollbar by writing XAML like such:
<ListView ScrollViewer.ScrollChanged="ScrollChanged" />
How to attach it to MVVM light Command? I tried the following, but it doesn't work:
<ListView>
<i:Interaction.Triggers>
<i:EventTrigger EventName="ScrollViewer.ScrollChangedEvent">
<cmd:EventToCommand Command="{Binding ScrollCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>
Note: ScrollCommand - is a RelayCommand from my viewmodel.
EventTrigger doesn't trigger for routed events. You can use the solution proposed in this article
to create a RoutedEventTrigger class and use it instead of EventTrigger.
I recommend a Behavior for this. If you don't have Blend, you'll need to get the Blend SDK. But once you have that, you can follow this tutorial to extend the behavior of the ScrollViewer.

how to implement control events in wpf using MVVM?

I have a slider control in view i need to call the ValueChanged event of slider and update one property in model based on that. how i can implement this using MVVM pattern.
where I have to write the ValueChanged event? how i can connect ValueChanged event code with view?
i just take the answer from here cause i use it in my projects too:
You should use an EventTrigger in combination with InvokeCommandAction from the Windows.Interactivity namespace. Here is an example:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectedItemChangedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>

Show calender on TextBox.GotFocus() event in MVVM

I have an application which is having TextBox. Upon getting the focus, I need to show the Calendar as Popup.
My question is how to show subscribe the GotFocus event and show the calendar through view model?
It is fairly acceptable to write code-behind for view-specific tasks like this one, however if you insist to have clean code-behind files , do the following
you will need MvvmLight.Extras.WPF4.dll and System.Windows.Interactivity.dll, the second DLL comes mainly with blend , google the first and at any case you can find them both on the MVVMLight package.
reference them as follows:
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
see your textBox
<TextBox>
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus">
<cmd:EventToCommand Command="{Binding showCalendar, Mode=OneWay}" MustToggleIsEnabledValue="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
on your view model you should have a property that is bound to your Calendar Visibility property , change it to Visible inside the method invoked by the command.
You really don't need to go to the ViewModel for this - it can be done very simply in XAML. Use a BooleanToVisibilityConverter on a binding that is attached to the TextBox's IsFocused property.
<TextBox x:Name="_textBox" Text="{Binding Text}" />
<myNameSpace:Calendar Visibility="{Binding ElementName=_textBox, Path=IsFocused, Converter={x:Static _boolToVisibilityConverter}, Mode=OneWay}" />

Resources