I have the following XAML:
<Window x:Class="thumb_test.MainWindow" Title="MainWindow" ... >
<Grid>
<Canvas>
<Thumb Canvas.Top="25" Canvas.Left="25" Width="50" Height="50"
Name="_thumb1" DragStarted="ThumbStart" DragDelta="ThumbMoved" >
</Thumb>
</Canvas>
</Grid>
</Window>
And the following is the corresponding code-behind:
void ThumbStart(object sender, DragStartedEventArgs e)
{
_originalLeft = Canvas.GetLeft(_thumb1);
_originalTop = Canvas.GetTop(_thumb1);
}
void ThumbMoved(object sender, DragDeltaEventArgs e)
{
double left = _originalLeft + e.HorizontalChange;
double top = _originalTop + e.VerticalChange;
Canvas.SetLeft(_thumb1, left);
Canvas.SetTop(_thumb1, top);
_originalLeft = left;
_originalTop = top;
}
The above displays a rectangle, which can be dragged around on the canvas.
My question: How can I associate this Thumb with a TextBlock, such that the Thumb overlays the TextBlock (with the Thumb being transparent) and I can drag the TextBlock around? (PS: Believe me, what I have tried so far is not worth showing here.)
My ultimate goal is to be able to drag TextBlocks around, so I am open to other approaches. I would like to operate on a Canvas, though.
I am using VS2010 on Win 7, with .NET 4.0.
have you read Dragging Elements in a Canvas ?
or this easy way(that i never saw until now -google) How to make any UI element drag-able using Behaviors in WPF
Related
I am a bit new to WPF. I wanted to make my image scrollable when I scale up the image. This is my XAML code.
<UserControl xmlns: skia="clr-namespace:SkiaSharp.Views.WPF:assembly=SkiaSharp.Views.WPF">
<Grid>
<skia:SKElement Name="Canvas" PaintSurface="SKElement_PaintSurface">
</Grid>
</UserControl>
private void SKElement_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
\\Code goes here...
}
Inside the SKElement_PaintSurface method, I created a way to draw the bitmap image on the canvas. But when I scale up the image I can't scroll the image. Does anyone know to create a scrollbar for this?
Put the Canvas in a ScrollViewer element and set its Height and/or Width when you scale it:
<UserControl xmlns: skia="clr-namespace:SkiaSharp.Views.WPF:assembly=SkiaSharp.Views.WPF">
<Grid>
<ScrollViewer>
<skia:SKElement Name="DrawCanvas" PaintSurface="SKElement_PaintSurface">
</ScrollViewer>
</Grid>
</UserControl>
private void SKElement_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
\\Code goes here...
DrawCanvas.Height = 100;
}
How to achieve an automatic full screen that not hides the taskbar, with no resize and no window style?
I tried to use the following code, but it doesn't work right, as shown in the image below.
XAML:
<Window x:Class="WPF_Test_Environment.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowState="Maximized" ResizeMode="NoResize" WindowStyle="None">
<DockPanel>
<Button DockPanel.Dock="Top" Height="50">Top</Button>
<Button DockPanel.Dock="Bottom" Height="50">Bottom</Button>
<Button DockPanel.Dock="Left" Width="50">Left</Button>
<Button DockPanel.Dock="Right" Width="50">Right</Button>
<Button Width="50" Height="50">Center</Button>
</DockPanel>
</Window>
Code-Behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
MinHeight = SystemParameters.MaximizedPrimaryScreenHeight;
MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
MinWidth = SystemParameters.MaximizedPrimaryScreenWidth;
}
}
Here is the result:
As you can see, the "Bottom" button is partially underneath the taskbar, and I want it to be entirely above it. So, ideally, it would look as shown in the following image, but without the border on the top:
It can be done with calls to unmanaged code. Check this article to see how to do it. Basically, just remove your width and height settings from code-behind, implement the WindowSizing class from the article and call it from SourceInitialized event of the window.
Edit
The other solution would be to add reference to Windows Forms and use:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Set the width and height values to the values of working area
this.Width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
this.Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
// Move the window to top left corner of the screen
this.Left = 0;
this.Top = 0;
}
Just make sure to remove WindowState="Maximized" from your window.
Not sure if any of these is elegant though.
I am working on a windows phone RT XAML c# project. I have a canvas filled with several dynamically added balls(ellipse) as ContentControl. I want that, when a ball is tapped, only that
particular ball should animate. I have used storyboard animation in the animating function animateBall() and the animation function works fine. But, I am not able to map the tapped
event of a single contentcontrol to the animating function animateBall().
I used foreach loop, but it is applying the animation to all the balls.
XAML:
<Canvas x:Name="playArea" HorizontalAlignment="Left" Height="624" Margin="10,10,0,0" VerticalAlignment="Top" Width="430">
<ContentControl x:Name="ball" Canvas.Left="167" Canvas.Top="482" Height="105" Tapped="ball_Tapped">
<Ellipse x:Name="ellipse" Fill="#FFE81B1B" Height="100" Stroke="Black" Width="100"/>
</ContentControl>
</Canvas>
c#
bool tapped;
foreach (UIElement ball in playArea.Children)
{
ball.Tapped += ball_Tapped;
double top= Canvas.GetTop(ball);
if (tapped)
{
animateBall(ball, top, playArea.ActualHeight, "(Canvas.Top)");
}
}
void ball_Tapped(object sender, TappedRoutedEventArgs e)
{
tapped = true;
}
I think you should try something like this:
foreach (UIElement ball in playArea.Children)
{
if(ball is ContentControl)
ball.Tapped += ball_Tapped;
}
}
void ball_Tapped(object sender, TappedRoutedEventArgs e)
{
ContentControl ball = sender as ContentControl;
double top = Canvas.GetTop(ball);
animateBall(ball, top, playArea.ActualHeight, "(Canvas.Top)");
}
I have little problem.
In my WPF appliaciton i have expander
>>some content
<Expander ExpandDirection="Right" Header="" Margin="341,6,-6,0" BorderBrush="{x:Null}" Foreground="Black" Canvas.ZIndex="-1">
<StackPanel Background="#FFE2E2E2" Margin="-28,30,-4,-2" Width="175" Opacity=".8">
>>some content<<
</StackPanel>
</Expander>
</Grid>
</Window>
When you eject it covers the original content, its good, but content is invisibly covered if are expander is hidden too and content under it can not use.
My think is: If expander is hidden - it have z-index: -1 and normal content can use, if I eject it - expander have z-index:1
but I do not know how to do it, thank you for reply.
I got it.
Stackpanel normaly have zindex -1, and tools on my window working.
if i click to the stackpanel for open it,
stack panel get zindex +1, if i close it (i must click to expander button again) stackpanel get zindex -1
you can see at code
public int indexx = -1;
private void StackPanel_Click(object sender, RoutedEventArgs e)
{
if (indexx == 1)
{
indexx = -1;
Canvas.SetZIndex(panel, indexx);
}
else
{
indexx = 1;
Canvas.SetZIndex(panel, indexx);
}
}
This question already has answers here:
Is there any way I can integrate the MS Office Smooth Typing in a C# application?
(2 answers)
Closed 9 years ago.
I was wondering, if I can make a TextBox or any control that you can write some text on it, to be like Word 2013, the animation experience is very good.
I am now able to do type of animation on the control itself (TextBox,...), but to do this type of animation to the cursor or on the text itself this is new.
You could create a WPF UserControl or custom control which inherits from the default WPF textbox. I was able to create a textbox that animates the cursor position with the following method:
1-Create a user control and add a textbox to it.
2-Add a canvas with a rectangle inside it (the rectangle is your new cursor).
3-Set the Texboxes CaretBrush to transparent.
4-In the UserControl's code-behind create a method to animate the cursor when the cursor position changes.
5-Call the animation method from step 4 when certain events happen which would change the cursor position.
Example:
UserControl XAML
<UserControl
x:Class="YourNamespace.AnimatedCursorTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="23"
d:DesignWidth="300"
xmlns:c="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
Name="Control">
<UserControl.Resources>
<c:BooleanToVisibilityConverter
x:Key="BoolToVisibility" />
</UserControl.Resources>
<Grid>
<TextBox
Name="MainTextBox"
CaretBrush="Transparent"
SelectionChanged="MainTextBox_SelectionChanged"
TextChanged="MainTextBox_TextChanged"
GotKeyboardFocus="MainTextBox_GotKeyboardFocus" />
<Canvas
Visibility="{Binding IsKeyboardFocusWithin,
ElementName=Control,
Converter={StaticResource BoolToVisibility}}"
Height="{Binding ActualHeight, ElementName=MainTextBox}"
Width="{Binding ActualWidth, ElementName=MainTextBox}">
<Rectangle
HorizontalAlignment="Left"
Name="Caret"
Width="1"
Fill="Black" />
</Canvas>
</Grid>
</UserControl>
Code-Behind:
public partial class AnimatedCursorTextBox : UserControl
{
private DoubleAnimation cursorAnimation = new DoubleAnimation();
public AnimatedCursorTextBox()
{
InitializeComponent();
}
private void UpdateCaretPosition()
{
var rectangle = MainTextBox.GetRectFromCharacterIndex(MainTextBox.CaretIndex);
Caret.Height = rectangle.Bottom - rectangle.Top;
Canvas.SetTop(Caret, rectangle.Top);
Canvas.SetBottom(Caret, rectangle.Bottom);
var left = Canvas.GetLeft(Caret);
if (!double.IsNaN(left))
{
cursorAnimation.From = left;
cursorAnimation.To = rectangle.Right;
cursorAnimation.Duration = new Duration(TimeSpan.FromSeconds(.05));
Caret.BeginAnimation(Canvas.LeftProperty, cursorAnimation);
}
else
{
Canvas.SetLeft(Caret, rectangle.Right);
}
}
private void MainTextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
UpdateCaretPosition();
}
private void MainTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
UpdateCaretPosition();
}
private void MainTextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
UpdateCaretPosition();
}
}
Note: This isn't a comprehensive solution because it doesn't handle the animation of highlighting selected text and it doesn't hide the cursor when text is highlighted, but it is a start. I recommend creating this as a custom control inheriting from TextBox and then do the layout of the control in the textbox's default style's template rather than using a UserControl. That way you can preserve all the functionality of TextBox but still get the new animation features. For more information about Custom Controls in WPF see this article on codeproject.
To change the speed of the animation just change the duration of the cursorAnimation.