DataGrid Cell with text and button - c#

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...

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!

SlectedItem when I click in a textbox or button in Datatemplate of a listview

I have a textbox and a button in a datatemplate as follows:
<DataTemplate x:Key="MyTemp1" x:DataType="data:Test">
<StackPanel Width="400">
<TextBox x:Name="tBoxName" Text="{Binding PhoneNumebr, Mode=TwoWay}" />
<Button x:Name="myButton" Content="textbutton" Click="myButtonButton_Click"/>
</StackPanel>
</DataTemplate>
and here my listView:
<ListView x:Name="myList" ItemTemplate="{StaticResource MyTemp1}"
SelectionChanged="myList_SelectionChanged"
IsItemClickEnabled="False"/>
for a listView in an UWP app, so I need a solution, that when I select the textbox or button, then selecteditem is accrued. In fact selectionchanged event is not fired when textbox or button is selected. is there any way to get selected item when textbox or button is selected? thanks
My problem has been solved as MikeBMcL explaind at this ilink:
https://social.msdn.microsoft.com/Forums/windowsapps/en-US/a5191023-fcdd-4e94-924e-6dee8e888267/uwp-xaml-button-on-list-item?forum=wpdevelop

WPF DataGrid CancelEdit from within DataGridTemplateColumn

I'm stuck on what seems to be a basic DataGridTemplateColumn question.
I have created a WPF DataGrid (sample code below) with a DataGridTemplateColumn. Inside the DataGridTemplateColumn, I have created a UserControl for the CellEditingTemplate. Within this UserControl, I have a button (and/or want to monitor a keypress, etc) that I want to use to Cancel (or Commit) the changes to the datagrid cell.
How do I go about notifying the DataGrid to CancelEdit from within the UserControl?
<DataGrid
AutoGenerateColumns="False"
ItemsSource="{Binding Items}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Text}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding Text}"/>
<Button Content="Cancel!">
<!--
How to make this Button Cancel Editing?
Click="Cancel"
Command="{Binding CancelCommand}"
-->
</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
The easy way to do it is to give the grid a name (theGrid), add a click handler to the button like this...
<Button Content="Cancel!" Click="Button_Click">
...and then cancel editing in the handler:
private void Button_Click(object sender, RoutedEventArgs e)
{
theGrid.CancelEdit();
}
The "correct" way to do it is to create a view model with a command handler, bind the button's Command property to it and then use an attached behavior that watches an event in your view model and triggers the cancel when it fires.

Telerik grid selection with a Button - MVVM

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

How to determine listboxitem a button was pressed from?

Basically I'm dynamically adding items to a listbox. Inside of each listbox item I have also added a button control that will perform a specific action on the item from which the button was clicked.
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Title"/>
<Button Click="MyBtn_Click"/>
</StackPanel>
</DataTemplate>
Does anyone know how to determine which item the button was clicked from? I know each listbox item contains an index. I think if you could access the parent of the button you could then deterimine which button was clicked?
You could use the CommmandParameter of the button to hold a value -
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Title}"/>
<Button Click="MyBtn_Click" CommandParameter={Binding Title}/>
</StackPanel>
</DataTemplate>
public void MyBtn_Click(object sender, args)
{
string MyVal = (sender as Button).CommandParameter.ToString();
}
Convention is to use the Command event instead of the click event for this type of thing.

Categories