how to pass checked event to button click in wpf mvvm - wpf

i have option button binded to some property and everything is working fine but now i want to achieve same thing on button click .
is there any way to pass click event to button click?my option button xaml code is as follows
<RadioButton Content="Any connection"
x:Uid="saifeconnect.settings.autoconnectto.any.radiobutton"
Margin="0,5"
GroupName="AutoConnect"
IsChecked="{Binding Path=AutoConnectMode,
Mode=TwoWay,
Converter={StaticResource enumConverter},
ConverterParameter=kAny}" />

If possible then make use of RelayCommand. Follow the given tutorial, best for RelayCommand.
https://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute

Related

How add a complete Click MouseGesture to a ListBoxItem in XAML

I have a ListBoxItem with a LeftClick Gesture:
<ListBoxItem x:Name="ListViewItemMenu" Content="{Binding Path=Header}" Padding="37 14" FontSize="15" Foreground="White">
<ListBoxItem.InputBindings>
<MouseBinding Gesture="LeftClick" Command="ApplicationCommands.New" />
</ListBoxItem.InputBindings>
</ListBoxItem>
But actually it's not a real Click, it's simply a MouseLeftButtonDown Gesture. The Command is executed regardless of whether the button is raised or not after pressed.
I would like to have a complete Click with the MouseDown and MouseUp Gestures in the ListBoxItem. There is anyway to do that? Much appreciated.
Also I can't find a way to add a Command to an Expander. The method shown above to the ListBoxItem doesn't work. Any magic here? Thanks!
According to this answer you can inherit from MouseGesture to create custom gestures. There is even a full example with implementation.
An alternative is to use an System.Windows.Interactivity.EventTrigger as shown in this answer, which would let you turn the MouseLeftButtonUp or PreviewMouseLeftButtonUp event into a command trigger.

WPF bind a control visibility to the focused property of another control

I have a combobox that displays a list of items, and I want to place a button next to it which triggers a command to see the details of the selected item. So far, so good. Now I want the button to be visible only if the combobox has focus (or is in "edit" mode, but not only when the popup is open).
I thought I could bind the visibility of the button to some focus property of the combobox, something like this:
<Button Content="Details" Visibility="{Binding ElementName=elementListComboBox,
Path=IsFocused, Converter={StaticResource Bool2VisibilityConverter}}"/>
But I found no way to know if the control I want is focused or not. I looked at the FocusManager.FocusedElement, but I don't know how to get the focused control I want inside the binding. Is there a way to achieve this in XAML?
Ok, the way to get this working as I wanted is this:
<Button Command="{Binding SomeCommand}"
Content="Details"
Focusable="False"
Visibility="{Binding ElementName=elementListComboBox,
Path=IsKeyboardFocusWithin,
Converter={StaticResource Bool2VisibilityConverter}}"/>
Two key factors here: bind the button's visibility to IsKeyboardFocusWithin property of the combobox, and set the button's Focusable property to false, else it will get collapsed when you want to click on it.
Hope this is useful.

Copy Property to Clipboard

I have a string property in my ViewModel/Datacontext and want a simple button that copies its contents to the clipboard. Is this possible to do from XAML, or I do I need to handle the button click event (or use an ICommand) to accomplish this? I thought the following would work, but my button is always greyed out:
<Button Width="100" Content="Copy" Command="ApplicationCommands.Copy"
CommandTarget="{Binding MyStringProperty}"/>
The ApplicationCommands are expecting to be in a Toolbar or Menu which will give them FocusScope based on RoutedUICommands. If your button is outside a Toolbar or Menu, then you need to explicitly declare the focus scope:
<Button
Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
Command="ApplicationCommands.Copy"
FocusManager.IsFocusScope="True"/>
The CommandTarget is used to declare which element will provide the FocusScope which means that the Copy button will only be enabled whenever the element declared in the CommandTarget has focus, and in the case of copy, has text highlighted:
<Button
Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
Command="ApplicationCommands.Copy"
CommandTarget="{Binding ElementName=MyElement}" />
In answer to your specific question, you'd need to intercept the ApplicationCommands.Copy command to get/set your ViewModel's MyStringProperty; and to be honest, I'm not sure where to even start to figure that one out. Maybe someone smarter around here could provide that piece of the puzzle.

Getting a templated button's command to work

I've used a control template to change the appearance of a button in a trivial way. It now looks different, but does not behave like a button. There are really two problems:
The button's command is never executed
After clicking on the button, it appears selected (i.e., the ellipse turns into an ugly blue rectangle)
Here's the general idea:
<Button Command="{x:Static commands:...}"
CommandParameter="{Binding}">
<Button.Template>
<ControlTemplate TargetType="Button">
<Ellipse Fill="{Binding ...}"
.../>
</ControlTemplate>
</Button.Template>
</Button>
There's no reason this should be happening. I put together a test using ApplicationCommands.Copy and the command fired just fine. Could be your CommandBinding isn't working properly.
I also didn't see this based on copying your sample XAML and just setting Fill="Green". You can try setting FocusVisualStyle="{x:Null}" on the Button.
The problem turned out to be that Fill was bound to a value that could be null. If the Fill brush is null rather than transparent, then there's nothing to click and the command doesn't get executed. As Drew mentioned, with a solid fill, the button works correctly.
Takeaway lesson: if you want to hide your shape but still have it respond to user interaction, use a transparent brush, not a null brush.
I had a similar problem with a custom templated button:
<my:UniButton Command="{Binding MyCommand}"/>
The binding didn't work until adding a RelativeSource:
<my:UniButton Command="{Binding MyCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=my:CustomPanel}}"/>
where CustomPanel is a control where my button lies.
Withal I had a simple button on the same panel, but it worked fine even without RelativeSource.

WPF DataGrid Button Column Disable

Is it possible to disable a button in a DataGridTemplateColumn? I have a DataGridTemplate as follows:
<toolkit:DataGridTemplateColumn Header="Timer" Width="50">
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Start" Click="Button_Click" CommandParameter="{Binding}" />
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>
The purpose of the Button is to start a timer recorded to the object associated with that row. My timer code works fine but I would also like to disable the buttons of every other row so that you can only have one timer running.
I used
WorkItemGrid.Columns[WorkItemGrid.Columns.Count - 1].GetCellContent(item).IsEnabled = false
to disable it and all the buttons correctly appear disabled but if you click on the button twice it will reenable and allow you to click on it a third time and trigger the Click event. Is it possible to actually disable the button?
I would have the object the Datagrid is bound to expose a "IsEnabled" boolean property I can bind the button to. Whenever the handler is called, simply get the other object from your original collection and have them change their property to false. This will automatically disable the other buttons.
If your are not in control of the "timer" class, you can wrap it in your own class before databinding the grid to the collection of your objects.
If it is acceptable to disable the button that was clicked in addition to the others, then I would bind Button.IsEnabled to a property that is set to false once the timer is started, and then change it back to true once the operation has finished.
If that is not acceptable, then I'm not sure if there is a way to do this, since by definition the template is used to create the controls in each row. Well, you could search the visual tree for all the other buttons, but that just doesn't seem like a good thing to do (not to mention it could be slow for a large amount of data).
its not working in my case.
<DataGridTemplateColumn Width="70" Header="Refund" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="btnRefundGame" IsEnabled="{Binding RESUND_STATUS}" CommandParameter="{Binding Path=IDEN_LOGID}" Content="Refund" Click="btnRefundGame_Click" ></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Resources