In WPF app inside a TabControl there is a TabItem element with one Label. I want to fire the event on MouseClick on the Tab of this TabControl, but the event MouseLeftButtonDown:
<TabItem Header="Header1" MouseLeftButtonDown="TabItem_MouseLeftButtonDown" >
<Label Height="28" Name="AdderLbl" Width="120" Background="Azure" >Label</Label>
</TabItem>
fires only on click over the label. It is not wat I want.
How I could fire the event on MouseClick over the Tab, not its content?
What are you trying to achieve? It looks like you're trying to trap when the user moves to the tab, if so you can hook a different event SelectionChanged
:-)
I did not want selectionchanged event because that would involve a switch. So i wanted an event directly on the tab clicked.
While i thought an MouseDown event was what I was looking for.... after wiring up all sorts of mouse events with break points; i found the MouseUp events worked for me.
So I used MouseLeftButtonUp or MouseUp.
For who really want to use something like MouseLeftButtonDown event, you can use the "MouseUp" event instead.
=)
While handling SelectionChanged event is the better approach to handling tab changes, here is a possible solution to your question. I don't recommend it in your case, but it illustrates an approach that may be useful in other cases
<TabControl>
<TabItem>
<TabItem.Header>
<Button Click="Button_Click">
<Button.Template>
<ControlTemplate>
<Label>Header1</Label>
</ControlTemplate>
</Button.Template>
</Button>
</TabItem.Header>
<Label Height="28" Name="AdderLbl" Width="120" Background="Azure" >Label</Label>
</TabItem>
</TabControl>
You need the event Selector.Selected
<TabControl x:Name ="TabControls" Dock="Top" DockWidth="500" DockHeight="500" TabItemShape="Rounded">
<TabControl>
TabControls.SelectionChanged += SelectionChanged;
private void SelectionChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs) {
var itemTab = (TabItem) TabControls.SelectedItem;
(...)
}
Related
I'm using RadioButton on a WPF/C# on a parameter view. I want the user to choose only 1 option, so I use RadioButton and GroupName. It works fine, but it only react to click event, not touch...
Is there a way to have touch event to control my buttons or do I have to redefine all the default comportment of a RadioButton with touchDown event ?
Here are my radioButtons, even if I don't think they are usefull for my problem
<Viewbox Height="50">
<RadioButton x:Name="imposerOrdreHaut" Content="Avec" IsChecked="True" GroupName="ordre" Padding="2" Checked="RadioButtonChecked" />
</Viewbox>
<Viewbox Height="50">
<RadioButton Content="Sans" GroupName="ordre" Padding="2" Checked="RadioButtonChecked" />
Finally I did it using a touchDown event who switch "IsChecked=true", and it automatically switch false other buttons
I have a TabControl with two TabItems, inside the one TabItem I have a DataGrid.
I'm trying to handle the TabItem click, and it works, but, when I click in one row of the "dataGrid1" the event "TabItem_MouseLeftButtonUp" of TabItem click is fired too. See the code:
<TabControl Height="211" HorizontalAlignment="Left" Margin="33,29,0,0" Name="tabControl1" VerticalAlignment="Top" Width="417" >
<TabItem Header="tabItem1" Name="tabItem1">
<Grid />
</TabItem>
<TabItem MouseLeftButtonUp="TabItem_MouseLeftButtonUp">
<DataGrid AutoGenerateColumns="True" Height="134" Name="dataGrid1" Width="307" />
</TabItem>
</TabControl>
Note: I can't use the personalize <TabItem.Header> because I'm using MahApps, if I use TabItem.Header the style os TabItem will break.
The MouseLeftButtonUp event is bubbling routed event. When you on the DataGrids row the event bubbling through its ancestors and calls the corresponding handlers, TabItem_MouseLeftButtonUp for TabItem in your case.
In your TabItem_MouseLeftButtonUp event you can check who raised the event, which control is the origin. If its not the TabItem do nothing.
private void TabItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if(sender is TabItem)
{
//do the work
}
}
You will recieve the EventArgs with MouseLeftButtonUp event. Just filter out whatever you need.
Is there a way to assign a click event to images? I would like to assign events to the delete and search buttons inside of my listbox that displays my data. Is there a way to do this using the image control or do I have to create a style in BLEND for a button?
<ListBox x:Name="lbPills" ItemsSource="{Binding pillItemsCollection}" SelectionChanged="lbPills_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
<TextBlock FontFamily="Segoe WP Semibold" FontWeight="Bold" FontSize="30" VerticalAlignment="Top" Margin="20,0">*</TextBlock>
<StackPanel>
<TextBlock x:Name="ItemText" Text="{Binding Name}" Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
</StackPanel>
<Image Source="Images/delete.png" Margin="10,0"/>
<Image Source="Images/search.png" Margin="10,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
To my knowledge there are no listeners on the Image itself for click and gesture events (they will have to be attached via Gestures as previously mentioned). One way to approach this is to re-template the button:
<Button>
<Button.Template>
<ControlTemplate>
<Image Source="Images/delete.png" Margin="10,0"/>
</ControlTemplate>
</Button.Template>
</Button>
In setting the template on the button you will override the default template used by the phone (which has the extra padding, thick border, etc.). Using this method will allow you to tie into the button click event.
You can use the gesture listener to detect tap (click) events. A walkthrough here.
WP7 Tip of the Day: Silverlight Toolkit: Gestures
Alternatively, you can place your image into a Button control and retemplate it in blend to have the appearance you want.
Handle the ManipulationCompleted event (which is any tap, double-tap, swipe, caress or fondle) to your image(s). So:
<Image Source="Images/delete.png" Margin="10,0"/> becomes <Image x:Name="ImageDelete" ManipulationCompleted="ImageDelete_ManipulationCompleted" Source="Images/delete.png" Margin="10,0"/>. Then in the ImageDelete_ManipulationCompleted handler, track from whence it came in from the sender and do your thing.
If you want to only track a swipe instead of a tap, just do an if statement on the e.IsInertial from ManipulationCompletedEventArgs.
If your ListBox is not in a Panorama control, then you could just handle the SelectionChanged event for the list box and then display the Delete and Search options in an ApplicationBar for the page.
I do it with MouseLeftButtonDown and MouseLeftButtonUp. It replaces the tab or click on the device like you have click event on pc with mouse. It works in my app. Just try this and you will be happy, I guess.
It worked this way for me (Making Padding="-10" removed the button border and padding inside the button)
<Button x:Name="Channells" Click="Thumb_Click" Padding="-10" >
<Image
VerticalAlignment="Center"
Source="Assets/Images/thumb2.jpg"/>
</Button>
*.cs
void Thumb_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Thumb Clicked");
}
I would like to prevent an Expander from expanding/collapsing when users click inside the header area. This is basically the same question as Q 1396153, but I'd appreciate a more favorable answer :)
Is there a non-invasive way to do this? I am not sure exactly how to attach behavior to the Expander.Header content to prevent mouseclicks. I'm willing to float in content outside the expander itself via a fixed grid layout, but I'm not keen on the solution. Ideas?
XamlPad sample XAML:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Expander>
<Expander.Header><TextBlock>
When I click this text,
I don't want to trigger expansion/collapse! Only when I click the
expander button do I want to trigger an expand/collapse!
</TextBlock></Expander.Header>
<Grid Background="Red" Height="100" Width="100" >
</Grid>
</Expander>
</Page>
You can stop mouse clicks on the text box from being handled by your application.
XAML:
<Expander>
<Expander.Header>
<TextBlock MouseDown="TextBlock_MouseDown">
When I click this text,
I don't want to trigger expansion/collapse! Only when I click the
expander button do I want to trigger an expand/collapse!
</TextBlock>
</Expander.Header>
<Grid Background="Red" Height="100" Width="100" >
</Grid>
</Expander>
Code behind:
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
I don't know if this is an abomination, but, I've moved the content out of the Expander.Header and done some Grid/fixed layout/Panel.ZIndex trickery to make it appear that the content is in the Expander.Header...but it's not. This works, but it's horrible.
I wanted to create a button that had an image and a textblock as content. So I went about looking for an answer and found a post (Reusable Custom Content for Buttons) which told me to create a usercontrol.
I did this and it works great. I can set the image source and text through dependency properties. However, I am stuck as there is no click event for my control.
I did a little more digging and concluded that I probably need a CustomControl derived from Button. Is this correct? Or would it be better to wire up a click event to my UserControl?
Here's my UserControl:
<UserControl x:Class="Client.Usercontrols.MyButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MinHeight="30" MinWidth="40"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Button Width="Auto" HorizontalAlignment="Center">
<Border CornerRadius="5" BorderThickness="1" BorderBrush="Transparent" >
<Grid>
<Image Name="tehImage" Source="{Binding ImageSource}" />
<TextBlock Name="tehText" Text="{Binding Text}"
Style="{DynamicResource ButtonText}" />
</Grid>
</Border>
</Button>
</UserControl>
Implementation
<my:MyButton ImageSource="../Images/MainSyncButton.png" ImageWidth="141" Text="Synchronise" Click="btnSynchronise_Click" />
The easiest option would be to just make your UserControl expose a click event, and pass through your Button's click event.
In MyButton's xaml:
<Button Width="Auto" HorizontalAlignment="Center" Click="onButtonClick">
In MyButton's code:
public event RoutedEventHandler Click;
void onButtonClick(object sender, RoutedEventArgs e)
{
if (this.Click != null)
{
this.Click(this, e);
}
}
You can then leave your "implementation" code as-is.
The answer really depends on what your goals are for the control. You may be able to get away with not creating a user or custom control if you can manipulate the data that you are binding to. If all you want to do is display a dynamic image and text, then you could create an ImageText object that contains two properties. You could then bind the default Button control's Content property to this object and use a DataTemplate to define the layout of the content.
If you cannot control the data type that you are binding to, or if you're really set on the idea of creating a control then I would recommend creating a custom control. Custom controls allow you to utilize the built-in capabilities of a standard button. Generally you would only want to create a User Control if you wanted to hide or encapsulate the default functionality of the visual controls contained within the control.
Good luck.