milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. C#

Programatically binding two text boxes

Details
Written by: Stanko Milosev
Category: WPF
Published: 21 January 2014
Last Updated: 30 November -0001
Hits: 4982

XAML:

<Window x:Class="WpfApplication2.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">
    <Grid>
        <TextBox x:Name="t1" HorizontalAlignment="Left" Height="23" Margin="101,43,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="t2" HorizontalAlignment="Left" Height="23" Margin="101,91,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Window>

Code:

        public MainWindow()
        {
            InitializeComponent();
            Binding binding = new Binding();
            binding.Source = t1; 
            binding.Path = new PropertyPath(TextBox.TextProperty);
            BindingOperations.SetBinding(t2, TextBox.TextProperty, binding);
        }

Drag'N'drop

Details
Written by: Stanko Milosev
Category: WPF
Published: 14 January 2014
Last Updated: 14 January 2014
Hits: 5240

First add a canvas:

Set width and height to auto:

Add a button.

Now comes a tricky part, at least for me, since I don't know how to delete margins from Visual Studio GUI, so I had to do it manually from XAML. So, here is my XAML:

<Window x:Class="DragNdrop.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">
    <Grid>
        <Canvas Margin="20" HorizontalAlignment="Left" VerticalAlignment="Top">
            <Button Content="Button" 
                    PreviewMouseLeftButtonDown="Button_PreviewMouseLeftButtonDown" 
                    PreviewMouseLeftButtonUp="Button_PreviewMouseLeftButtonUp" 
                    PreviewMouseMove="Button_PreviewMouseMove"/>
        </Canvas>
    </Grid>
</Window>

Notice that margins from button node are deleted, and margin is equal 20 on canvas (Margin="20"), otherwise button will be away from mouse pointer.

Code looks like this:

public partial class MainWindow : Window
{
	private double FirstXPos;
	private double FirstYPos;
	private double FirstArrowXPos;
	private double FirstArrowYPos;
	private object MovingObject;
	public MainWindow()
	{
		InitializeComponent();
	}

	private void Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
	{
		FirstXPos = e.GetPosition(sender as Control).X;
		FirstYPos = e.GetPosition(sender as Control).Y;
		FirstArrowXPos = e.GetPosition((sender as Control).Parent as Control).X - FirstXPos - 20;
		FirstArrowYPos = e.GetPosition((sender as Control).Parent as Control).Y - FirstYPos - 20;
		MovingObject = sender;
	}

	private void Button_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
	{
		MovingObject = null;
	}

	private void Button_PreviewMouseMove(object sender, MouseEventArgs e)
	{
		if (e.LeftButton == MouseButtonState.Pressed && sender == MovingObject)
		{
			(sender as Control).SetValue(Canvas.LeftProperty,
				 e.GetPosition((sender as Control).Parent as Control).X - FirstXPos - 20);

			(sender as Control).SetValue(Canvas.TopProperty,
				 e.GetPosition((sender as Control).Parent as Control).Y - FirstYPos - 20);
		}
	}
}

Code you can download from here, dirty as usual and made in VS 2013

Multiline text box

Details
Written by: Stanko Milosev
Category: WPF
Published: 21 October 2013
Last Updated: 21 October 2013
Hits: 5149
<Grid>
	<TextBox Height="194" HorizontalAlignment="Stretch" Name="textBox1" VerticalAlignment="Bottom" TextWrapping="Wrap" AcceptsReturn="True" />
</Grid>

Most important values are:

TextWrapping="Wrap"

and

AcceptsReturn="True"

Or in properties:

and

Read XML from Azure blob storage

Details
Written by: Stanko Milosev
Category: WPF
Published: 30 November -0001
Last Updated: 14 October 2013
Hits: 1577

After creating new WPF project install Windows Azure Storage through NuGet:

You will need ServiceRuntime also, but that you have to install manually. Right click on "References":

Then click on browse and go, in my case, to C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\2012-10\ref. Of course, first you have to install Azure SDK.

In my case, since I need this just as a sample application, add one button, and create click event for that button (double click on the button).

In same file (in my case \WpfApplication3\MainWindow.xaml.cs) add next three lines in your using list:

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.ServiceRuntime;
  1. Treeview with Autofac
  2. IsNullOrEmpty
  3. Unsafe code may only appear if compiling with /unsafe
  4. Example of WebClient

Subcategories

WPF

Beginning

Code snippets

NUnit

LINQ

Windows Forms

Page 33 of 39

  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37