I have a Grid placed at the top of the Window in which inside of it there's an Image placed. The Grid is supposedly dragging the window with a MouseDown event.
However, whenever i want to fire a MouseDown event to the Child Image it doesn't work but, instead it fires the Grid's.
private void toggleTbr_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
private void leapTcb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
//my code
}
As you see i tried e.Handled = true; but it changes nothing, then i thought of trying of using PreviewMouseLeftButtonDown instead of MouseLeftButtonDown but, still the same.
What am i doing wrong here, or how to prevent the Grid from triggering?
XAML:
<Grid x:Name="toggleTbr" MouseLeftButtonDown="toggleTbr_MouseLeftButtonDown">
<Grid x:Name="leapTcb_" Height="21" Width="26">
<Image x:Name="leapTcb" MouseLeftButtonDown="leapTcb_MouseLeftButtonDown">
<Image.Background>
<ImageBrush Source="Resources/leap_1.png"/>
</Image.Background>
</Image>
</Grid>
</Grid>
Your Code works fine, but if it isn't enough you can try using this:
IsHitTestVisible="True"
The Image element has no Background property, instead, set the Source property directly at the image element.
<Image x:Name="leapTcb" Source="Resources/leap_1.png" MouseLeftButtonDown="leapTcb_MouseLeftButtonDown"/>
However you made this running, but with the specified change it behaves like you want, at least on my machine.
Use this this is working my side
<Grid x:Name="toggleTbr" MouseLeftButtonDown="toggleTbr_MouseLeftButtonDown" Background="Red">
<Grid x:Name="leapTcb_" Height="21" Width="26">
<Image x:Name="leapTcb" PreviewMouseLeftButtonDown="leapTcb_MouseLeftButtonDown">
<Image.Background>
<ImageBrush Source="Resources/leap_1.png"/>
</Image.Background>
</Image>
</Grid>
</Grid>
Related
I have a Control that contains a Popup. I am trying to close the Popup whenever someone clicks outside of the Control. This is the part of my code that sets up the problem:
AddHandler(Mouse.PreviewMouseDownOutsideCapturedElementEvent, new MouseuttonEventHandler(HandleOutsideClick), true);
Now whenever I click in the Popup it causes PreviewMouseDownOutsideCapturedElementEvent to be raised. Am I misunderstanding this event? Is there something that I can do to have the Popup be considered a part of the Control so that it doesn't raise this event?
Does this work?
<Popup Name="Pop" LostFocus="ClosePop"/>
private void ClosePop(object sender, RoutedEventArgs e)
{
Pop.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
Put the XAML code in your .xaml page and the C# code in the related .xaml.cs file.
Note: You may need to put the focus on the popup before this works, it may be done automatically; I haven't done this on popups, but I have done it on other objects.
Update: This works for me, clicking in the TextBox that says Test1 opens the Popup, and clicking in the TextBox labeled Test2 closes it:
<Grid Background="White">
<StackPanel>
<TextBox Foreground="Black" LostFocus="ClosePop" GotFocus="OpenPop" Height="50">Test1</TextBox>
<TextBox Foreground="Black" Height="50">Test2</TextBox>
</StackPanel>
<Popup Name="Pop" Height="50" Width="50">
<TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center">Pop!</TextBlock>
</Popup>
</Grid>
private void ClosePop(object sender, RoutedEventArgs e)
{
Pop.IsOpen = false;
}
private void OpenPop(object sender, RoutedEventArgs e)
{
Pop.IsOpen = true;
}
I have an Ellipse in my WPF application. I want to change the colour of its outline whenever it is double clicked. I found this (old) tutorial about making this work by using the available MouseDown event and checking for a ClickCount of two in the event handler. This is the simplest solution to my problem and I'd like to try and get this to work before creating an empty button Control Template.
However, I'm unable to find the clicked ellipse in my code behind file. Supposedly this works in the tutorial, but I'm wondering if I'm missing anything.
Here's the code that contains the ellipse. It is the 3rd column of a grid:
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.Column="3">
<StackPanel Orientation="Vertical" Margin="3,1" Background="GhostWhite">
<ItemsControl Name="FlowLinkItems" ItemsSource="{Binding FlowLinkList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Height="40">
<Ellipse Name="FlowLinkEllipse" Stroke="BlueViolet" Height="38" VerticalAlignment="Center" MouseDown="Ellipse_MouseDown"/>
<TextBlock TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Message}"></TextBlock>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
In the tutorial, the code behind method worked like this:
private void Ellipse_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
FlowLinkEllipse.Stroke = "Red";
}
}
And the error I'm seeing is:
The name 'FlowLinkEllipse' does not exist in the current context
If this method is not possible I'm open to suggestions that are as simple as possible (I'm still new to WPF and the only thing my app will handle is this double click).
Note: I do have this line in my code behind and it works fine.
FlowLinkItems.MouseLeftButtonUp += FlowLinkItems_MouseLeftButtonUp;
As #Magus noted, you can't reference an item from code-behind, that is inside a DataTemplate. That should be no problem here, though: sender will contain a reference to the ellipse:
private void Ellipse_MouseDown(object sender, MouseButtonEventArgs e)
{
if (ellipse as sender == null || e.ClickCount < 2)
return;
var ellipse = (Ellipse)sender;
ellipse.Stroke = System.Windows.Media.Brushes.Red;
}
Here I have sample window with a grid. I need to capture event when key is pressed. But it is not raising when I click grid area and then press key. It will work only if Textbox is focused. I know it will work if I capture it from Window. But I have other application with few usercontrols and I need to capture it from distinct ones. I tried to set Focusable.false for Window and true for Grid but it not helps.
Any solutions?
<Window x:Class="Beta.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Closed="Window_Closed_1" Focusable="False">
<Grid KeyDown="Grid_KeyDown_1" Focusable="True">
<TextBox x:Name="tbCount" HorizontalAlignment="Left" Height="35" Margin="310,49,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="83"/>
</Grid>
Right this is weird. This is clearly a focus problem, still I can not understand why the grid do not take Focus, even when we click on it.
Though there is a workaround: create an handler for the loaded event of the grid:
<Grid x:Name="theGrid" KeyDown="Grid_KeyDown_1" Focusable="True" Loaded="TheGrid_OnLoaded">
And then force focus in your code behind:
private void TheGrid_OnLoaded(object sender, RoutedEventArgs e)
{
theGrid.Focus();
}
Your keydown event will work after that.
Hope it helps.
I had the same issue with a Universal Windows Platform (UWP) app. I attached the event to a grid in XAML but it would only work when the focus was on the TextBox. I found the answer (not just a workaround) on MSDN: https://social.msdn.microsoft.com/Forums/en-US/56272bc6-6085-426a-8939-f48d71ab12ca/page-keydown-event-not-firing?forum=winappswithcsharp
In summary, according to that post, the event won't fire because when focus to the TextBox is lost, it's passed higher up so the Grid won't get it. Window.Current.CoreWindow.KeyDown should be used instead. I've added my event handlers to the page loaded event like this:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
Window.Current.CoreWindow.KeyDown += coreWindow_KeyDown;
Window.Current.CoreWindow.KeyUp += CoreWindow_KeyUp;
}
This works as expected for me.
I tried using the Focus Method too, to no avail, until I set the Focusable property to true ( It was default to False. )
I had the same problem, I've used PreviewKeyDownevent and it worked for me.
how can I access the arrow buttons on the ends of scrollbars in silverlight? Are they button controls which can be accessesed through thescrollbar class or something else?
The reason I need to know this is so that when someone click on either of the arrow buttons I can run some custom functionality to the scrollbar
If you need to know what button of the scrollbar was clicked you can could access that via the ValueChanged property of the ScrollBar
For instance I've got a simple scroll bar with a textblock and on each click, the textblock displays which button was clicked.
<Grid x:Name="LayoutRoot" Background="White">
<ScrollBar Height="200" Orientation="Vertical" Width="20" ValueChanged="ScrollBar_ValueChanged" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="150,21,0,0" Name="textBlock1" Text="" Width="100" VerticalAlignment="Top" />
</Grid>
and the code behind would be
private void ScrollBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (e.NewValue > e.OldValue)
textBlock1.Text = "Down Arrow Clicked";
else
textBlock1.Text = "Up Arrow Clicked";
}
Solved this problem like so:
foreach( var o in horizontalBar.GetVisualDescendants( ) )
{
if(o is RepeatButton)
{
//set call back based on the name of the repeatbutton
}
}
I am new in this field and am trying to do a drag and drop from scatterview to librarystack, and when drop is executed, an event should be triggered ( after an image is dropped in the librarystack, a message box will be shown). However, I can not find the way to do it..
Below is the code snippets
<Grid>
<!-- Drop 1.jpg to librarystack-->
<s:ScatterView AllowDrop="True">
<Image Source="C:\1.jpg"/>
</s:ScatterView>
<!--Librarystack get the 1.jpg and trigger an event-->
<s:LibraryStack x:Name="myLibraryStack" Drop="myLibraryStack_Drop" Margin="376,0,389,0" Background="Transparent" Height="162" VerticalAlignment="Top" AllowDrop="True">
</s:LibraryStack>
</Grid>
and
public SurfaceWindow1()
{
InitializeComponent();
SurfaceDragDrop.AddDropHandler(myLibraryStack, OnPreviewDrop);
}
private void myLibraryStack_Drop(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("HALLO");
}
Would appreciate for any guidance
I think that the LibraryStack has to be inside the Scatterview to receive the drop. But I'm not sure. I had the problem myself once and this solved it, if I remember correctly.