Telerik grid selection with a Button - MVVM - c#

I've got a telerik RadGridView with several rows and columns. One of the columns contain a button, which call a command of my ViewModel.
Here's my columns description:
<telerikGrid:RadGridView.Columns>
<telerikGrid:GridViewColumn Width="32" IsVisible="{Binding IsAvailable}">
<telerikGrid:GridViewColumn.CellTemplate>
<DataTemplate>
<telerik:RadButton Width="24" Height="24" x:Name="AddItemToList"
Command="{Binding AddItemToListCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}">
<Image Stretch="Uniform" Source="Images/add.png" HorizontalAlignment="Center" />
</telerik:RadButton>
</DataTemplate>
</telerikGrid:GridViewColumn.CellTemplate>
</telerikGrid:GridViewColumn>
<telerikGrid:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
<telerikGrid:GridViewDataColumn DataMemberBinding="{Binding ConName}"/>
<telerikGrid:GridViewDataColumn DataMemberBinding="{Binding DisName}"/>
</telerikGrid:RadGridView.Columns>
When a row is selected and I click on the button, the good values are sent to the ViewModel. But, my problem is that, if I click on the button of another row (not my selected row), it does not select the row and thus, send the wrong values to VM (the value of the selected row is sent)!
How do I force the row selection to select the row containing the button I am clicking?
It should actually be done in my XAML, before my command is executed.
Hope its clear enough. I want my focus to be set on the row containing the button I am clicking.

You can send the Selecteditem as the CommandParameter of AddItemToListCommand
CommandParamter="{Binding}". So you will get row datacontext i.e the item on which button was clicked.
Thanks

Related

How can I disable the selection event on this WPF ComboBox without disabling it?

I have the following ComboBox XAML:
<ComboBox Width="350" ItemsSource="{Binding RunSets}" SelectedItem="{Binding SelectedRunSet, Mode=OneWay}" VerticalAlignment="Center" Height="20" Margin="5,1,0,0" Background="DimGray" Foreground="White" >
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid Width="320">
<CheckBox IsChecked="{Binding IsSelected}" Margin="2,2,10,0" Visibility="{Binding ExchangeMarketNamesVisibility}" PreviewMouseDown="RunsetComboBoxItem_PreviewMouseDown">
<CheckBox.Content>
<StackPanel PreviewMouseDown="RunsetComboBoxItem_PreviewMouseDown">
<StackPanel Orientation="Horizontal" PreviewMouseDown="RunsetComboBoxItem_PreviewMouseDown">
<TextBlock Text="{Binding DisplayName}" PreviewMouseDown="RunsetComboBoxItem_PreviewMouseDown"/>
</StackPanel>
<StackPanel PreviewMouseDown="RunsetComboBoxItem_PreviewMouseDown">
<TextBlock Text="{Binding ExchangeMarketNames, StringFormat=🢒 {0}}" Visibility="{Binding ExchangeMarketNamesVisibility}" Margin="2,2,0,2" Foreground="LightGray" PreviewMouseDown="RunsetComboBoxItem_PreviewMouseDown" />
</StackPanel>
</StackPanel>
</CheckBox.Content>
</CheckBox>
<TextBlock Text="Select Items..." Visibility="{Binding DefaultItemVisibility}"/>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The goal I'm trying to achieve is to have the ComboBox present a set of selectable ComboBoxItems. It should always display "Select Items" when closed:
And when opened I would like it to display the following:
When the highlighted area is clicked it should check the checkbox without closing the combobox. I have managed to get most of the behaviour I want by hacking the Visibility property to hide the checkbox in the "Select Items..." ComboBoxItem. Together with capturing the PreviewMouseDown event:
private void RunsetComboBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
var checkBox = sender as CheckBox;
if(checkBox != null)
{
checkBox.IsChecked = !checkBox.IsChecked;
e.Handled = true;
}
}
This is almost working perfectly, but there are two issues that I can't resolve:
If I click on the border between the ComboBoxItems in the dropdown it will select that ComboBoxItem. I want it to never select any of them. I have managed to work around for clicks inside the highlighted area by capturing the preview mouse down event on the Grid in the and marking it handled. Unfortunately in the border this doesn't get triggered and the SelectedItemChanged event gets triggered on the ComboBox. This causes the selected item to change (as mentioned, I want the ComboBox to always display the "Select Items..." ComboBoxItem.
The "Select Items..." is shown twice when the combo box is expaned. I would like it to show just once.
I have managed to get most of the behaviour I want by hacking the Visibility property to hide the checkbox in the "Select Items..." ComboBoxItem together with capturing the PreviewMouseDown event.
I have read through many stack overflow posts about this, and I have not found a way of doing this. I tried capturing the SelectionChange and marking it handled but it closed the combobox anyway, and there is no PreviewSelectionChange event. When an item is selected I'd like the combobox to stay open.
Is there any way I can achieve this? Please let me know if there's any more information I can add to clarify my problem. Thanks!

DataGrid Cell with text and button

In a cell of a DataGrid there is a "add" button and then a textblock show quantity. When the user clicks on the "add" button it adds one to the quantity. I am having a hard time figuring out how to create the textblock. Here is my cell;
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="+" Click="addQTYButton_Click"/>
<TextBlock Text="{Binding qty}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
The best solution would be not to use the Click event and the code-behind.
Use a command instead and in your ViewModel change the string the TextBlock is bound to when the command is invoked...
If you insist using the event and the code-behind, just name the TextBlock and it will be available to you in the event handler to manipulate it any way you like...

Displayed text in WPF combo box with MenuItems and radio buttons

I have a combo box with radio buttons and checkable menu items. When I select a value some text of the radio buttons (Include / Exclude / Ignore) is displayed. This seems to be randomly and does not happen always, but only sometimes.
What I want is that a) no text at all is displayed or b) I can have a binding for a string property, which will contain some summary of what is selected. Unlike in normal combo boxes, the displayed text here is meaningless, since I do select some arbitrary combination of options (not a single value out of a list)
I have tried to bind Text / SelecteValue against a property, but this does not work.
<ComboBox Grid.Column="2" HorizontalAlignment="Left" Margin="0,4,0,0" Grid.Row="3" VerticalAlignment="Top" Width="125" SelectedValue="{Binding Settings.NameFilterTrx.Summary, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}">
<MenuItem Header="Names with runway" IsCheckable="True" IsChecked="{Binding Settings.NameFilterTrx.NamesWithRunway, UpdateSourceTrigger=PropertyChanged}"/>
<MenuItem Header="Names with number" IsCheckable="True" IsChecked="{Binding Settings.NameFilterTrxNamesWithNumber, UpdateSourceTrigger=PropertyChanged}"/>
<MenuItem Header="Filtered elements as comments" IsCheckable="True" IsChecked="{Binding Settings.NameFilterTrx.FilteredAsComments, UpdateSourceTrigger=PropertyChanged}"/>
<RadioButton GroupName="Group" Content="Exclude" IsChecked="{Binding Settings.NameFilterTrx.ModeExclude, UpdateSourceTrigger=PropertyChanged}" Margin="10,2,0,3"/>
<RadioButton GroupName="Group" Content="Include" IsChecked="{Binding Settings.NameFilterTrx.ModeInclude, UpdateSourceTrigger=PropertyChanged}" Margin="10,2,0,3"/>
<RadioButton GroupName="Group" Content="Ignore" IsChecked="{Binding Settings.NameFilterTrx.ModeIgnore, UpdateSourceTrigger=PropertyChanged}" Margin="10,2,0,3"/>
</ComboBox>
As you can see the text does not correspond with the selected value. Also only text of the radio buttons is randomly displayed, never something of the menu items.
PS: The binding for SelectedValue is one of my trails to get a defined value. If have tried Text as well as no binding.
I think you could use an expander to fullfill your goal. The problem with this solution is that when the Expander is expanded, it may change the size of your parent container. So what I usually do is to put it in a Grid and specify it to span several rows and columns. Then I will set the z-index of the Expander to a large value. So the expanded Expander will overlay the other controls and not change the size of the parent panel. :)

How to disable some button of wpf datagrid

I m having a WPF DataGrid. in which there is one column contains edit button so how to enable or disable some button of datagrid progrmaticaly for example
<DataGrid AutoGenerateColumns="True" Height="80" Margin="2,-4,8,0" Name="grdQHDRShowAll" VerticalAlignment="Top" ItemsSource="{Binding}" IsReadOnly="True" SelectionChanged="grdQHDRShowAll_SelectionChanged">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="Qhdr_CreateOrder_Click" CommandParameter="{Binding Path=QuoteNo}" >Create Order</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
the above code have a column with buttons so i want to know how to disable some buttons in rows depends on other columns data like true and false value of second column, if second column first row have data true then first column first rows button should be enable means depends on any column other column how to eable/disable button of datagrid ?
If youre using the MVVM pattern you can bind the IsEnabled Property of the Button to a boolean Property of your VM or you use a converter to convert it to a boolean type:
<Button IsEnabled="{Binding Path=BoolProp}"/>
If not, you can bind it to an element using elementbinding:
<Button IsEnabled="{Binding ElementName=CellName, Path=BoolProp}"/>
<Button Content="Approve" VerticalAlignment="Center" Height="23" Width="90" FontWeight="Bold" Command="{Binding ApprovedCommand}" Margin="5,0,0,0" IsEnabled="{Binding IsEnabled,Mode=TwoWay}"/>
In MVVM you create a property and make IsEnabled true or false on condition

How to get selectedItem from ListView from a button click in the listView Item

I have a list view item which has a button and displays properties on an object. I have the command property set up for the button using the mvvm pattern which fires correctly.
My issues is that when a press the button it does not also get the selected item, I am guessing that it has not fired the selectedItem event.
Is it possible to force this event when the button is pressed?
<Button
Height="20"
Width="20"
Grid.Column="3"
Grid.Row="0"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.RemoveClick}"
CommandParameter="{Binding SelectedItem, ElementName=serverListView, Mode=TwoWay}">
You may :
- set the commandParameter of your command to {Binding}
- Bind the SelectedItem to some Property of your VM
- Set this property to the command parameter in the begining of your command execute method

Categories