Pages

Friday 8 March 2013

Button Control in WPF

Button Control In WPF

The Button element represents a WPF Button control in XAML.
 <Button></Button>

The Width and Height attributes of the Button element represent the width and the height of a Button. The Content property of the Button element sets the text of a button. The x:Name attribute represents the name of the control, which is a unique identifier of a control.
 
 <Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1">
    <Grid>
        <Button Margin="71,136,77,86" Name="button1">Button</Button>
    </Grid>
</Window>

Adding a Button Click Event Handler
 
The Click attribute of the Button element adds the click event handler. The following code adds the click event handler for a Button.


<Button x:Name="Random Number" Click="RandomNumber_Click">
</Button>
 
The Code of the Windows.xaml : 

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Button Height="23" Margin="94,0,108,80" Name="OK" 
VerticalAlignment="Bottom" Click="button1_Click">Button</Button>
        <TextBlock Height="21" Margin="61.812,42.723,96.354,0"
 Name="textBlock1" VerticalAlignment="Top" />
    </Grid>
</Window> 
 
Code for Button Click Event-
 
private void button1_Click(object sender, RoutedEventArgs e)
        {
            Random generator = new Random();
            int randomValue;
            randomValue = generator.Next(1, 10);
            textBlock1.Text += " " + randomValue.ToString();
        } 
 
Properties of Button

Name
Description
AllowDrop Gets or sets a value indicating whether this element can be used as the target of a drag-and-drop operation.
AreAnyTouchesCaptured Gets a value that indicates whether at least one touch is captured to this element.
AreAnyTouchesCapturedWithin Gets a value that indicates whether at least one touch is captured to this element or to any child elements in its visual tree.
AreAnyTouchesDirectlyOver Gets a value that indicates whether at least one touch is pressed over this element.
AreAnyTouchesOver Gets a value that indicates whether at least one touch is pressed over this element or any child elements in its visual tree.
CacheMode Gets or sets a cached representation of the UIElement.
ClickMode Gets or sets when the Click event occurs.
Clip Gets or sets the geometry used to define the outline of the contents of an element.
ClipToBounds Gets or sets a value indicating whether to clip the content of this element (or content coming from the child elements of this element) to fit into the size of the containing element.
Command Gets or sets the command to invoke when this button is pressed.
CommandBindings Gets a collection of CommandBinding objects associated with this element. A CommandBinding enables command handling for this element, and declares the linkage between a command, its events, and the handlers attached by this element.
CommandParameter Gets or sets the parameter to pass to the Command property.
CommandTarget Gets or sets the element on which to raise the specified command.
Content Gets or sets the content of a ContentControl.
ContentStringFormat Gets or sets a composite string that specifies how to format the Content property if it is displayed as a string.
ContentTemplate Gets or sets the data template used to display the content of the ContentControl.
ContentTemplateSelector Gets or sets a template selector that enables an application writer to provide custom template-selection logic.
InputBindings Gets the collection of input bindings associated with this element.
InputScope Gets or sets the context for input used by this FrameworkElement.
     

1 comment: