I create a button with image in my app:
<Button x:Name="favoriteButton" HorizontalAlignment="Left" VerticalAlignment="Top" Height="72" Width="99" Click="DidPressAddToFavorites" BorderBrush="{x:Null}" Foreground="{x:Null}">
<Button.Background>
<ImageBrush ImageSource="/Images/favouritesBWIcon#2x.png" Stretch="Uniform"/>
</Button.Background>
</Button>
And i noticed that when the user press the button all the button became blue and when i release the button i see it again. any idea how to fix it?
Edit:
This is the handler method:
private void DidPressAddToFavorites(object sender, RoutedEventArgs e)
{
if (favoriteRep.ExistInFavorites(currentItem) == true)
{
this.SetButtonWithImage(favoriteButton, "/Images/favouritesBWIcon#2x.png");
favoriteRep.RemoveFromFavorites(currentItem);
}
else
{
this.SetButtonWithImage(favoriteButton, "/Images/favouritesIcon#2x.png");
favoriteRep.AddToFavorites(currentItem);
}
}
Because you didn't add the States, there are three states Normal, MouseOver, Pressed... You have to set the image for all three states, to make it work of your wish. Here the alternate to do so, didn't know about any better way available from c#, all you have to do is to add two more eventHndlers Mouse over event and press event, then set the image in both of them....
Well there's another good way to do so is try template editing from blend Software
Related
I am currently developping an interface that shows multiple camera shot and the direct view of the camera.
What i need to do is put a label on a picturebox (picturebox that refresh camera live every frame).
My problem is that when the picturebox refresh, the label get under it.
Here's how my XAML code is made :
<Grid x:Name="GridCam4" Grid.Column="1" Grid.Row="1" Margin="0,0,24,0">
<WindowsFormsHost x:Name="DisplayWindow" Foreground="#FFA0A0A0" Background="#FFA0A0A0" Panel.ZIndex="2" Height="435" Width="678">
<forms:PictureBox x:Name="picturebox1" BackgroundImageLayout="None"/>
</WindowsFormsHost>
<Label x:Name="LabelLive" Content="Live :" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="102" Width="206" FontSize="36" Panel.ZIndex="12" Foreground="White"/>
</Grid>
And there is my C# code :
private void onFrameEvent(object sender, EventArgs e)
{
uEye.Camera Camera = sender as uEye.Camera;
Camera.Memory.GetLast(out s32MemID);
//Display the image on the screen (Refresh the picturebox with last picture taken)
Camera.Display.Render( DisplayHandle, uEye.Defines.DisplayRenderMode.FitToWindow);
Camera.Memory.Unlock(s32MemID);
this.Dispatcher.Invoke(
new Action(() =>
{
LabelLive.Content = "Live :"; //Trying to refresh the label to put it over picturebox
}),
DispatcherPriority.ApplicationIdle);
}
I really don't know what to do to deal with this...
I have also tried ZPanels but nothing worked at the moment.
Thanks for your help !
I have created custom WPF toolbox control. I have implemented drag and drop functionality, which is working as intended but when i move the mouse (dragging data) over the drop target, the mouse cursor is displayed like this action cant be completed although code executes and is working correctly. (here you can see how the cursor looks when dragging over target). I have tried following in target OnDragEvent:
Mouse.OverrideCursor = Cursors.Hand; in that case the cursor only changes for like 1/1000 second and then changes back to the one that can be seen in the image above.
Cursor.Current = Cursors.Hand; which also doesnt work.
Take care you need to implement some events in case of drag and drop I advice to see the following link I think it will be useful and solve your problem.
drag amp drop in wpf explained end to end
Did you use DoDragDrop?
There you could set the Effect as a third parameter:
DragDrop.DoDragDrop(drg, dragData, DragDropEffects.Move);
Maybe this helps:
I have two Canvas with one holding a button:
<Canvas
HorizontalAlignment="Left"
Height="225"
Margin="270,10,0,0"
VerticalAlignment="Top"
Width="237"
PreviewMouseMove="Canvas_PreviewMouseMove">
<Button
Content="Button"
Canvas.Left="65"
Canvas.Top="65"
Width="75"/>
</Canvas>
<Canvas
HorizontalAlignment="Left"
Height="225"
Margin="20,10,0,0"
VerticalAlignment="Top"
Width="245"
AllowDrop="True"
Background="Black"
/>
and this in the PreviewMouseEvent:
private static readonly string ident = "test";
private void Canvas_PreviewMouseMove(object sender, MouseEventArgs e)
{
DataObject dragData = new DataObject(ident, ((Canvas)sender));
DragDrop.DoDragDrop((Canvas)sender, dragData, DragDropEffects.Move);
}
Here you get your Move effect when setting the Backcolor, if no color is set you don't get the move effekt. Maybe this is your issue.
(Same behavior for a UserControls)
I created a UserControl, and added a Button inside it removing the Background and Text properties:
<Button x:Name="Button"
HorizontalAlignment="Center"
Height="40"
VerticalAlignment="Center"
Width="40"
RenderTransformOrigin="0,-2"
Margin="0,0,0,0"
BorderBrush="{x:Null}"
Click="Button_Click"
Background="{x:Null}"/>
I also hadled the Button Click event as below:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button.Content = new cross();
}
The above code fills the Button content with another UserControl which is a simple cross pic.
I have placed the UserControl with the Button into a MainWindow app and after pressing Button, it starts blinking - background is fluently changing between two colours. Beside my functionality from code works good. I just don't know how to get rid of that blinking background.
Before click:
After click:
You could set Focusable="False" at your Button to achive this.
But you should read about the Focusableproperty in the MSDN to check if it's ok for you. I guess you can't focus the Buttonusing the tab key anymore. But maybe that's not a problem for you.
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>
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.