ItemClick event on part of GridView - c#

I have a collection of objects that I want to bind to my Xaml (using a GridView at the moment). I don't know if a GridView is the best tool for what I want to achieve.
This is roughly my current DataTemplate:
I need a clicked/tapped trigger on Grid1. But when I use this template with a GridView, the ItemClick event will get triggered on the complete template - which is correct of course. How can I set an click event on Grid1? Can I add this event in my DataTemplate declaration?
That's basically the xaml for my GridView:
<GridView x:Name="RoomsGridView"
VerticalAlignment="Top"
ItemsSource="{Binding Rooms}"
ItemTemplate="{StaticResource RoomTemplate}"
SelectionMode="None"
IsSwipeEnabled="false"
IsItemClickEnabled="True"
ItemClick="RoomsGridView_ItemClick" Padding="0">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
Edit: I've found this sample but this seems only to work with Buttons!?

In the code you posted above you've told to GridView to capture item clicks via IsItemClickEnabled="True". You cannot have that option enabled if you want to be able to click on elements inside the DataTemplate.
I would suggest that you disable IsItemClickEnabled and instead add a click handler to your Grid1. Even better would be to substitute your Grid1 with a button that has custom styling. This would further allow you to bind the button to a command that you can control from your view model.

Related

How to Change border of selecteditem

I have one main window.xaml, inside which i have referred Viewmodel and another xaml page called Template.xaml.
Mainwindow.xaml will be some thing like below:
<Window ...>
<Window.Resorce ../>
<Window.DataContext>
<local:Viemodel/>
</Window.DataContext>
<Grid...
<local:Template/>
</Grid>
</Window>
And this is my Tempaltes.Xaml:
<ItemsControl HorizontalAlignment="Left" ItemsSource="{Binding Path=ControlsList}" KeyboardNavigation.IsTabStop="False">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="950"
Margin="28"
KeyboardNavigation.IsTabStop="False"
Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Name="Mybutton" Height="200" Background="Red" Command="{Binding Path=SupportButton}">
.....
In tha above block i have used binding in 3 places.
1. Item Controls
2. Button
3. Image
And here the command binding was not working for "MyButton". What may the reason behind this?
I need to change the background of button once the particular item is chosen. How to achieve that?
Under the button properties there is additionally a tab for actions or action methods. You'll want to find the one that is a button clicked event.
Once you have created an event handler from that action you will have the ability to change the background or foreground of the button.
Not 10% sure what you're trying to achieve here, but perhaps a ListBox would work better for you?
It'll automatically change the background of the item while it's selected and remove the background when another item is selected. If you want to perform an action when the selected item changes you could bind to the SelectedItem property.
Just a thought...
It's difficult to tell from the question but it sounds like using a ToggleButton may be appropriate in your case. If you go that route, you could simply bind the IsChecked property to your View Model, and your property setter could perform any command-like logic.
And of course you could style the ToggleButton any way you like, so the IsChecked state could look relatively different from the Normal state.

How to UWP Print ListView ItemTemplate

I'm trying to figure out how to print a ListView ItemTemplate using the uwp PrintHelper.cs sample. Everything works, except the print preview does not display items added to the ListView at runtime. I can add other controls such as a textbox, and the print preview will show it, so there must be something peculiar with printing databound ListView items at runtime, but I cannot find any information about it.
<ListView x:Name="ClipboardList"
xmlns:m="using:QuickieEdit.Models"
ItemsSource="{x:Bind ViewModel.MemoryItems}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="m:MemoryItem">
<StackPanel Orientation="Horizontal">
<Button x:Name="MemoryCopyBtn"
Content="Copy"
Click="How to Copy currently selected
MemoryListItem.Text?"/>
<TextBox x:Name="MemoryListItem"
Text="{x:Bind Memory, Mode=TwoWay}">
</TextBox>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
I cannot understand your exact query, but I think you may be facing situation where your ListView does not get updated with the model and hence does not show up while printing. You can use
ObservableCollection<Model> instead of List<Model>
This will solve your problem or if it does not please provide the c# code in detail as well
Cheers

Telerik DragDrop Does not work

I've been developing a software and i want to use telerik Listbox drag&drag.
the problem is when i drop an item from one listbox to another it does not work at all.
It seems that the drop event does not fire.
I set allowdrop= true
I added dragvisualprovider
I set allowreorder = true
I also tried to write code when dragLeave and PreDrop Events Fire and add that item into the new Listbox. but it works sometimes correct by chance!!!
I don't use MVVM Model
based on what telerik site says, I don't use static items collection. I create new items in my page load and add it to first Listbox.
You have to use <telerik:ListBoxDragDropBehavior AllowReorder="True" /> like this:
<telerik:RadListBox ScrollViewer.VerticalScrollBarVisibility="Visible" SelectedItem="{Binding DataContext.SelectedItem, ElementName=editor,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ItemContainerStyle="{StaticResource DraggableListBoxItem}" DisplayMemberPath="DisplayName" ItemsSource="{Binding LeaveActions,Mode=TwoWay}" Grid.Row="3" AllowDrop="True" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<telerik:RadListBox.DragDropBehavior>
<telerik:ListBoxDragDropBehavior AllowReorder="True" />
</telerik:RadListBox.DragDropBehavior>
</telerik:RadListBox>
Than every thing should work well. If you want to get into the drag/drop events, just use the telerik DragDropmanager.

Picking the right ItemsSource container

I have a UserControl I've written to display a few properties from a custom object. There are multiple instances of these objects so I have an ObservableCollection of them so I can set them as an ItemsSource binding to a ListView. Now I can get an instance of this UserControl show up for each instance of my class in my ListView.
The problem is I don't really want the behavior of a ListView. I don't want the user to be able to select the entire UserControl. In fact, the user should be able to select individual elements in the UserControl.
I thought about just using a StackPanel to put these UserControls in, but it doesn't have an ItemesSource property. Is there an easy way to make this happen?
Replace your ListView with an ItemsControl and set the ItemTemplate to a suitable DataTemplate for your objects. You can set the ItemsPanel if you want to change how the panel lays out the items.
<ItemsControl ItemsSource="{Binding Items}"
ItemTemplate="{StaticResource ItemTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
See this example

WPF ListBox not updating with the ItemsSource

I have what I believe should be simple two-way databinding in WPF setup, but the listbox (target) is not updating as the collection changes.
I'm setting this ItemsSource of the ListBox programmatically:
lstVariable_Selected.ItemsSource = m_VariableList;
And the ListBox is declared as follows:
<ListBox Margin="5" Name="lstVariable_Selected">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Gray" BorderThickness="1" Margin="0">
<TextBlock FontSize="25" Text="{Binding Path=Name}" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When I initially set the ItemsSource, the ListBox (which is not visible at the time) gets its items set. However, if I go view the ListBox, updates seem to stop at that point.
I can then remove an item from the m_VariableList collection, and it does not disappear from the ListBox. Likewise, if I add one, it doesn't appear.
What gives?
Is your m_VariableList implementing INotifyCollectionChanged? If it's not an ObservableCollection, then changes to it's contents will not automatically be reflected in the UI.
The problem is not in the XAML that you have provided. I used the same XAML successfully in a test application; however, I was able to replicate the issue you are experiencing by re-instantiating the m_VariableList variable.
When the m_VariableList is given a new instance, or pointed to a new object, it is not reflected in the ListBox because the control has its own reference to the data. This may not be the cause of your problem, but I'd recommend looking over your code-behind to ensure that the variable is not getting re-instantiated.
i got stuck for more than hour and then simple logic solved this problem
just set itemsource to clear list and then set source u need again
lstVariable_Selected.ItemsSource = new List<Object>();
lstVariable_Selected.ItemsSource = m_VariableList;

Categories