Well. This is pretty easy. I am giving you a example. In this example you will see that I have used label and a button. When we click the button the label will Grow/Shrink. But i have maintain it from code behind
The xaml is given below
<Window x:Class="WpfTest.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 Height="311" Width="474">
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="188,263,0,0" Name="button1" VerticalAlignment="Top" Width="100" Click="button1_Click" />
<Label Content="Hi!!" Height="100" HorizontalAlignment="Center" Margin="188,76,186,0" Name="label1" VerticalAlignment="Top" Width="100" Background="#FFBBF23E" />
</Grid>
</Window>
And the code behind is given below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
Grid grd = btn.Parent as Grid;
Label lbl = grd.Children[1] as Label;
if (lbl.Width < 100)
{
lbl.Width = 100;
}
else
{
lbl.Width = 50;
}
if (lbl.Height < 100)
{
lbl.Height = 100;
}
else
{
lbl.Height = 50;
}
}
}
}
So What i am doing here is that I just get the Button Object and then the Parent of the Button Which is Grid. In this Grid the Second Child is My Label and Once we have the Label Object we could change its Property as we could do in the xaml.