I'm developing a windows store application with C# and XAML. I am using a ListView to display the collection of data.
Inside the ListView I have a data template which has grids and a TextBlock in the grid. I want to tap/click the TextBlock and give action without selecting the parent ListView item as I already have event to handle the selected ListView item. I don't want both to overlap.
Thanks in advance for any response.
So you want to be able to select the ListViewItem when tapping one part of it, but not the TextBlock? If this is the case, in the TextBlock's Tapped event add e.Handled = true;. This should make it so that it isn't further routed up to the parent ListView.
The other thing you can do (which will likely be a more general solution to whatever you want to do with your ListViewItems) is to not use thing SelectionChanged event and instead handle everything with ItemClick. You can then deduce whether the OriginalSource of the event is indeed your TextBlock. Then, if it's not the TextBlock, change the parent ListView's SelectedItem.
An example for chceking the OriginalSource
public static void ItemClickEvent(object sender, ItemClickEventArgs e)
{
if(e.OriginalSource is TextBlock)
DoNothingOrMaybeTextBlockEvent();
else
{
ListView.SelectedItem = e.ClickedItem;
}
}
Hope this helps.
Edit: Added some example code for the OriginalSource check
Related
I'm using Caliburn.Micro and the LongListSelector. Because binding SelectedItem is a problem I act on the SelectionChanged event. Problem is, after returning to the list, when I click the same item again, it is already selected. So the event doesn't fire. I could set the SelectedIndex to -1 or something, but in Caliburn.Micro I can't access UI controls. That's the point of MVVM, isn't it?! :)
Here's my XAML
<LongListSelector x:Name="NewsItems"
ItemsSource="{Binding NewsItems}"
cal:Message.Attach="[Event SelectionChanged] = [NavigateToArticle($eventArgs)]" />
How to solve this? How can I reset the SelectedItem when I can't access the LongListSelector from code?
Thanks!
You have to put your LongListSelector SelctionMode="Multiple",
or
You can get it using Gesture Tap event.
Did not understood real problem but
I think you can solve your problem if selection change event fire every time when user select item-
private void productList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LongListSelector productList= (LongListSelector)sender;
if (productList.SelectedItem == null)
return;
//Write your code here
//For Tapping many times..
productList.SelectedItem = null;
}
The above code will make selection change event to fire on selection of same item every time.
I have a problem with adding Item to FlipView dynamically, I have a very simple FlipView and I put these codes in SelectionChanged event:
private void myFlipView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TextBlock tb = new TextBlock();
myFlipView.Items.Add(tb);
}
the strange is when I try to swipe between pages with mouse rapidly it works, but if I swipe with finger it stops working and I have to swipe on the page slowly to make it work.
I wish I could express the problem clearly....
I am not sure if I understood correctly? What exactly do you want to achieve?
In your case you are adding a new FlipViewItem which contains a TextBlock every time you navigate throught the FlipView. So theoretically you will never reach the end of a FlipView.
I have a child window created in silverlight. I need to load a user control within the child window(for a content change in the same child window) on a button click.
How can i acheive this?
Say for Example: If i have a child window with a Header -> Content -> Button.
I just need to change the content part and the button part on click of the button.
I need to change the buttons also since navigation is not possible using the same button click events.
Is it possible to acheive this in Silverlight 4.0 or 5.0?
Here is one way to do it. Create a canvas to hold the content and on button click, add the user control and add it as a child to the canvas. If you want to change the button, rather than changing the button, in the same button click the button content to a different text.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
SilverlightControl1 control1 = new SilverlightControl1();
top.Children.Add(control1);
}
Hope this helps.
I am designing a settings form for my application as shown below:
A tree view with multiple nodes at the left and I want to have one GroupBox for each node to be displayed at the right whenever a node is selected. I have designed my group box with necessary controls for the first node. The question is, how do I design an another group box in the same place when another item is already there. Is there a way to hide a control from a form during design time?
I have always just changed the Z-Order of the GroupBox or Panel by right clicking on it and sending it to back. Just make sure when you add the other GroupBoxes that you add them to the same Parent. Make their Visible property False and then display the GroupBox you want at runtime by making it Visible.
EDIT: Changed answer to be more relevant.
EDIT #2: I missed the VB tag, translating this should be a trivial task anyway.
If you extend GroupBox like this, you'll have a stock GroupBox which will hide itself at design time.
public class myGroupBox : GroupBox
{
public myGroupBox() { InitializeComponent(); }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (DesignMode) this.Visible = false;
}
}
NOTE: This should work for almost any non-sealed control.
With a listbox visible, I have clicked on the windows form hoping to use the listbox.lostfocus event to let me hide the listbox - but the event does not occur. I suppose I can use the form.click event to hide the listbox, but how would I get the form to accept focus?
A Form does not want to receive the focus. It was designed to be a container control, it makes sure that one of its child controls always gets the focus. It is technically possible to whack it over the head and make it lose that behavior:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.SetStyle(ControlStyles.ContainerControl, false);
}
protected override void OnClick(EventArgs e) {
this.Focus();
base.OnClick(e);
}
}
This is however a bad idea. A Form doesn't have any way to indicate that it has the focus, you'll also have to override OnPaint() to do something like draw a focus rectangle. If you don't then the user completely loses track of where the focus is located. Then there's the considerable inconvenience that nothing interesting can happen when the user uses the keyboard, a form doesn't have a use for it.
Don't do this. If you want to make a control disappear then add a menu item, toolbar button or a normal button to your UI. Something the user can click on.
the LostFocus event work when the focus move to another control like textbox,... or when the form all of it lost the focus you can use click event for the form to detect taht