Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Friday, October 16, 2015

WPF: Set Startup Window.

You can set your Startup page by setting StartupUri="MainWindow.xaml"  in App.xaml file. 
<Application x:Class="MyApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MyWindow.xaml">
    <Application.Resources>
   
    </Application.Resources>

</Application>

Tuesday, October 13, 2015

WPF: How to stretch in width a WPF user control to its window?


Just Remove the width from UserControl it will automatically adjust the width.  

<UserControl x:Class="etc"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" >

WPF: DockPanel - When to use?



Dockpanels 


are useful when when placing and organising several different items onto a window, specifically when anchoring items to the top, bottom, left, right and then fitting to the remaining space in the centre (I recently discovered they're quite handy when used in conjunction with expanders). No real downsides, could well be effective for you.

XAML: 

<Window x:Class="WpfTutorialSamples.Panels.DockPanel"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DockPanel" Height="250" Width="250">
        <DockPanel>
                <Button DockPanel.Dock="Left">Left</Button>
                <Button DockPanel.Dock="Top">Top</Button>
                <Button DockPanel.Dock="Right">Right</Button>
                <Button DockPanel.Dock="Bottom">Bottom</Button>
                <Button>Center</Button>
        </DockPanel>
</Window>

Thursday, October 8, 2015

WPF: Styling a button in code.


Add Style in your App.xaml file (placed on the root of your project).

   <Application.Resources>
<Style x:Key="MyButton" TargetType="Button">
            <Setter Property="Background" Value="Red" />
            <Setter Property="Width" Value="100"></Setter>
            <Setter Property="Height" Value="100"></Setter>
 </Style>
   <Application.Resources>


In .CS file. 

  Button btn = new Button();
  Style style = this.FindResource("MyButton") as Style;
  btn.Style = style

WPF: Programmatically Add Click Event to a Button.


Sometime we create button on the fly and want to attach an event at runtime. 
so here we go. 

// Creating button on the fly.
 Button btn = new Button();
 // Passing an argument to the handler
 btn.CommandParameter= "0012" ;

// attaching handler to click event.
btn.Click += teamHandler;


// Handler for button
 protected void teamHandler(object sender, RoutedEventArgs args) {
            var btn = (Button)sender;
            int teamId = int.Parse(btn.CommandParameter.ToString());
            string name = Repository.GetItem(teamId).Name
            MessageBox.Show(name);
}

WPF: StackPanel Alignment

Set the attribute (Orientation) of StackPanel Horizontal or Vertical so all child controls will be aligned automatically. 

Orientation="Horizontal"
Or
Orientation="Vertical"

WPF: dynamically add images



This is how we can add an image dynamically in stackpanel.

XAML
<stackpanel name="Container"></stackpanel>

.CS (C#)
Image img = new Image();
img.Source = new BitmapImage(new Uri(@"pack://application:,,,/Quiz1;component/Images/loin.png"));
Container.Children.Add(img);

WPF Window Center Align

Set the attribute of window element.
WindowStartupLocation="CenterScreen"

WPF Set Window Background

<Window.Background>
 <ImageBrush ImageSource="Images/background.jpg" AlignmentY="Top"
 AlignmentX="Center"/>
</Window.Background>