11/20/2018

ASP.NET how to upload file to a server using class HttpPostedFileBase

Here I want to tell you about uploading a file to a server and storing it in a database.





























First we must create a local database file in the project.
















We also make a button to select the file on the disk.
Then we need to define the function Create() that writes the file to the database.
For this we will use a class HttpPostedFileBase.
Also add the ability to upload a file from the server using the function DownLoadFile().
Another function DeleteFile() will be responsible for deleting the file on the server.

All program code:


public class File
{
 public int Id { get; set; }
 public string FileName { get; set; }
 public string ContentType { get; set; }          
 public byte[] Content { get; set; }
}

public class HomeController : Controller
{
 DbUplFileContext db = new DbUplFileContext();

 public ActionResult Index()
 {
  return View(db.Files);
 }
  
 [HttpPost]
 public ActionResult Create(File model, HttpPostedFileBase someFile)
 {
  var f_file = new File();
  f_file.FileName = System.IO.Path.GetFileName(someFile.FileName);
  f_file.ContentType = someFile.ContentType;

  using (var reader = new System.IO.BinaryReader(someFile.InputStream))
  {
   f_file.Content = reader.ReadBytes(someFile.ContentLength);
  }

  db.Files.Add(f_file);
  db.SaveChanges();

  return RedirectToAction("Index", "Home");
 }

 public ActionResult DownLoadFile(int id)
 {
  File fileToRetrieve = db.Files.Find(id);
  return File(fileToRetrieve.Content, fileToRetrieve.ContentType, fileToRetrieve.FileName);
 }

 public ActionResult DeleteFile(int id)
 {
  File fileToDelete = db.Files.Find(id);
  if (fileToDelete != null)
  {
   db.Files.Remove(fileToDelete);
   db.SaveChanges();
  }
  return RedirectToAction("Index", "Home");
 }
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@model IEnumerable<File_upload.Models.File>

@{
    ViewBag.Title = "Home Page";
}
<br />
<br />

<style>
    table {
        border-spacing: 6px;
        border-collapse: separate;
    }

    td, th {
        border: solid 1px #ccc;
    }
</style>

<table>
    <tr>
        <td><p><h3>File name</h3></p></td>
    </tr>
    @foreach (File_upload.Models.File b in Model)
    {
        <tr>
            <td> <p>@b.FileName</p></td>
            <td> <p> <a href='@Url.Action("DownLoadFile", "Home", new { id = b.Id }, null)'>Download</a></p> </td>
            <td> <p> <a href='@Url.Action("DeleteFile", "Home", new { id = b.Id }, null)'>Delete</a></p> </td>
        </tr>
    }
</table>

<br />
<br />
<br />
<p><h3>Add File</h3></p>

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="someFile" />
    <br />
    <input type="submit" value="Upload" name="UploadButton" id="UploadButton" />
}

10/31/2018

The fastest way to generate tables in MS WORD

There are several ways to build a table in a MS Word document.
Not all of them are fast.
And some take a lot of time. Especially when the table has many lines.
I want to talk about the function ConvertToTable() that works very fast.

using System.IO;
using Microsoft.Office.Interop.Word;


//CREATING WORD AND DOCUMENT  
oApp = new Microsoft.Office.Interop.Word.Application();
oWordDoc = oApp.Documents.Open(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\Test_.docx");

Range rng = oWordDoc.Bookmarks["Bmark_in_table"].Range;  

string str = "";
for (int i = 0; i < 300; i++)
{
 str = str + "111111\t22222222\t33333333\t4444444\t5555\t7777\t8888\n";
}

rng.Text = str;

object missing = System.Reflection.Missing.Value;

object f_tab = WdTableFieldSeparator.wdSeparateByTabs;
object f_colWidth = oApp.CentimetersToPoints(1.7f);
Table f_table = rng.ConvertToTable(
  ref f_tab,
  ref missing, ref missing, f_colWidth, ref missing,
  ref missing, ref missing, ref missing, ref missing,
  ref missing, ref missing, ref missing, ref missing,
  ref missing, ref missing, ref missing
 );

object File = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\Test_RES.docx";
oWordDoc.SaveAs(ref File, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
oWordDoc.Close(ref missing, ref missing, ref missing);
oApp.Quit(ref missing, ref missing, ref missing);


10/04/2018

Event KeyDown not work

Event KeyDown is used in Winforms in the UI form.
Sometimes there is a problem when the event does not come.
Nothing happens when you press the keyboard keys.
Why this happens is not known.
One of the solutions to this problem is to turn on property KeyPreview.

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
 if (e.Control && e.KeyCode == Keys.O)
 {
  MessageBox.Show("Hello!");
 }
}

8/23/2018

How to create connection string in ASP.NET MVC application?

When we need to connect to the existing database, we need to edit the file Web.config and add the section <connectionStrings>.

To bind the Data context and Database, we use the <connectionString> section.
Inside this section, we write the connectionString attribute.
The value of this attribute can be taken by copying from the Data connection properties.

Go step by step:
Server Explorer > Data connection > BookContext > Properties > Connection String.

8/09/2018

How to use Winforms control Chart?


In this article, I'll tell you how to create a graph in the Winforms control Chart.
Our steps will be such:
  1. How to draw a linear graph?
  2. How to add a legend to the graph?
  3. How to scale the size of the graph (zoom)?

First you need to connect a library System.Windows.Forms.DataVisualization to work with Chart.








You also need to use namespace
using System.Windows.Forms.DataVisualization.Charting;

How to draw a linear graph?

chartTest.Dock = DockStyle.None;
            
chartTest.ChartAreas.Clear();
chartTest.Series.Clear();
chartTest.Legends.Clear();
chartTest.ChartAreas.Add("areaTest");
chartTest.ChartAreas["areaTest"].AxisX.MajorGrid.LineColor = Color.Black;
chartTest.ChartAreas["areaTest"].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Solid;  
chartTest.ChartAreas["areaTest"].AxisY.MajorGrid.LineColor = Color.Black;
chartTest.ChartAreas["areaTest"].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Solid;
chartTest.ChartAreas["areaTest"].BackColor = Color.White;

Color[] f_colour = new Color[] { Color.Green, Color.Red, Color.Blue, Color.Yellow, Color.YellowGreen, Color.HotPink };
int k = 0;

string f_nameSeries = "A1";
chartTest.Series.Add(f_nameSeries);
chartTest.Series[f_nameSeries].ChartArea = "areaTest";
chartTest.Series[f_nameSeries].Color = f_colour[k];
chartTest.Series[f_nameSeries].BorderWidth = 3;

for (int i = 0; i < 1000; i++)
{
    chartTest.Series[f_nameSeries].Points.AddXY(i, i);
}

How to add a legend to the graph?

chartTest.Series[f_nameSeries].ChartType = SeriesChartType.StepLine;
chartTest.Series[f_nameSeries].LegendText = "LegendText";
chartTest.Legends.Add(f_nameSeries);
chartTest.Legends[f_nameSeries].BorderColor = f_colour[k]; 

How to scale the size of the graph (zoom)?

Here we have 2 steps.

1. To create handler and allow series selection.

chartTest.MouseDown += new MouseEventHandler(clikOnGraf);

chartTest.ChartAreas["areaTest"].CursorX.IsUserSelectionEnabled = true;
chartTest.ChartAreas["areaTest"].CursorY.IsUserSelectionEnabled = true;

2. Write function that will reset zoom settings.

private void clikOnGraf(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        Chart f_ch = (Chart)sender;
        f_ch.ChartAreas["areaTest"].AxisX.ScaleView.ZoomReset();
        f_ch.ChartAreas["areaTest"].AxisY.ScaleView.ZoomReset();
    }
}

All program code:
private void Form1_Load(object sender, EventArgs e)
{
    chartTest.Dock = DockStyle.None;

    chartTest.ChartAreas.Clear();
    chartTest.Series.Clear();
    chartTest.Legends.Clear();
    chartTest.ChartAreas.Add("areaTest");
    chartTest.ChartAreas["areaTest"].AxisX.MajorGrid.LineColor = Color.Black;
    chartTest.ChartAreas["areaTest"].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Solid;
    chartTest.ChartAreas["areaTest"].AxisY.MajorGrid.LineColor = Color.Black;
    chartTest.ChartAreas["areaTest"].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Solid;
    chartTest.ChartAreas["areaTest"].BackColor = Color.White;

    Color[] f_colour = new Color[] { Color.Green, Color.Red, Color.Blue, Color.YellowGreen, Color.HotPink };
    int k = 0;

    string f_nameSeries = "A1";
    chartTest.Series.Add(f_nameSeries);
    chartTest.Series[f_nameSeries].ChartArea = "areaTest";
    chartTest.Series[f_nameSeries].Color = f_colour[k];
    chartTest.Series[f_nameSeries].BorderWidth = 3;

    for (int i = 0; i < 1000; i++)
    {
        chartTest.Series[f_nameSeries].Points.AddXY(i, i);
    }

    chartTest.Series[f_nameSeries].ChartType = SeriesChartType.StepLine;
    chartTest.Series[f_nameSeries].LegendText = "LegendText";
    chartTest.Legends.Add(f_nameSeries);
    chartTest.Legends[f_nameSeries].BorderColor = f_colour[k];

    chartTest.MouseDown += new MouseEventHandler(clikOnGraf);

    chartTest.ChartAreas["areaTest"].CursorX.IsUserSelectionEnabled = true;
    chartTest.ChartAreas["areaTest"].CursorY.IsUserSelectionEnabled = true;
}

private void clikOnGraf(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        Chart f_ch = (Chart)sender;
        f_ch.ChartAreas["areaTest"].AxisX.ScaleView.ZoomReset();
        f_ch.ChartAreas["areaTest"].AxisY.ScaleView.ZoomReset();
    }
}


8/06/2018

WPF animated PopUp menu

In this tutorial, we'll talk about how to create an animated popup menu in WPF.



















All the code we write in the XAML code and without using the codebehind. The external interface of the program is divided into several areas.
It will be the left part StackPanel, the central part Grid, the upper part StackPanel and the lower part Grid.
In the left part add four buttons. Also add animation DoubleAnimation that will work when you click on the button represented here.





In the bottom part we will place the Settings section.
When you start the program, the user can not see this section.It activates after pressing the button.






<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="Main" Height="400" Width="600" WindowStartupLocation="CenterScreen">

    <Grid x:Name="grid1" Margin="0,0,0,0"
           Background="#7385E5">

        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <StackPanel  x:Name="stackPanel1"
                    Background="#7385E5" 
                    Margin="-100,0,0,0" Width="100"
                    HorizontalAlignment="Left">
            <StackPanel.RenderTransform>
                <TranslateTransform x:Name="trans__TransStackPanel" X="0" Y="0" />
            </StackPanel.RenderTransform>
            <Grid Height="80">
            </Grid>
            <Button 
                     Margin="-100,0,0,0" Height="40" 
                     Width="100" HorizontalAlignment="Left" 
                     Background="#64E895" 
                     FontFamily="Wingdings" 
                     FontSize="24"
                     Content="&#x3A;">
                <Button.RenderTransform>
                    <TranslateTransform x:Name="trans__TransformButton1" X="0" Y="0" />
                </Button.RenderTransform>
            </Button>
            <Button   
                     Margin="-100,0,0,0" Height="40" 
                     Width="100" HorizontalAlignment="Left"
                     Background="#64E895"
                     FontFamily="Wingdings" 
                     FontSize="24"
                     Content="&#x37;">
                <Button.RenderTransform>
                    <TranslateTransform x:Name="trans__TransformButton2" X="0" Y="0" />
                </Button.RenderTransform>
            </Button>
            <Button  
                     Margin="-100,0,0,0" Height="40" 
                     Width="100" HorizontalAlignment="Left"
                     Background="#64E895"
                     FontFamily="Wingdings" 
                     FontSize="24"
                     Content="&#x3C;">
                <Button.RenderTransform>
                    <TranslateTransform x:Name="trans__TransformButton3" X="0" Y="0" />
                </Button.RenderTransform>
            </Button>
            <Button  
                     Margin="-100,0,0,0" 
                     Height="40" Width="100"
                     HorizontalAlignment="Left"
                     Background="#64E895"
                     FontFamily="Wingdings" 
                     FontSize="24"
                     Content="&#x38;">
                <Button.RenderTransform>
                    <TranslateTransform x:Name="trans__TransformButton4" X="0" Y="0" />
                </Button.RenderTransform>
            </Button>
        </StackPanel>
        <Grid x:Name="grid2"
                    Background="#66BEE0" 
              Margin="0,0,0,0" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>

            <Grid.RenderTransform>
                <TranslateTransform x:Name="trans__TransformGrid" X="0" Y="0" />
            </Grid.RenderTransform>

            <Button   
                Grid.Column="0" 
                Margin="10,10,0,0"
                Width="40" Height="40" 
                Panel.ZIndex="3"
                HorizontalContentAlignment="Center" 
                VerticalContentAlignment="Center"
                Padding="1,1,1,1" VerticalAlignment="Top"  
                Content="&#xDC;" 
                Background="{x:Null}"
                FontFamily="Wingdings"
                FontSize="30" 
                BorderBrush="{x:Null}"
                FontWeight="Bold" HorizontalAlignment="Left">

                <Button.Triggers>
                    <EventTrigger RoutedEvent="Button.Click">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard >
                                    <DoubleAnimation Storyboard.TargetName="trans__TransStackPanel"
                                    Storyboard.TargetProperty="X"
                                    From="0" To="100" 
                                    Duration="0:0:0.5" />

                                    <DoubleAnimation Storyboard.TargetName="trans__TransformGrid"
                                    Storyboard.TargetProperty="X"
                                    From="0" To="100" 
                                    Duration="0:0:0.5" />
                                    <DoubleAnimation Storyboard.TargetName="trans__TransformGrid"
                                    Storyboard.TargetProperty="Y"
                                    From="0" To="45" 
                                    Duration="0:0:0.5" />

                                    <DoubleAnimation Storyboard.TargetName="trans__TransformButton1"
                                    Storyboard.TargetProperty="X"
                                    From="0" To="100" 
                                    Duration="0:0:0.5" />
                                    <DoubleAnimation Storyboard.TargetName="trans__TransformButton2"
                                    Storyboard.TargetProperty="X"
                                    From="0" To="100" 
                                    Duration="0:0:0.5"  BeginTime="0:0:0.1" />
                                    <DoubleAnimation Storyboard.TargetName="trans__TransformButton3"
                                    Storyboard.TargetProperty="X"
                                    From="0" To="100" 
                                    Duration="0:0:0.5"   BeginTime="0:0:0.2"/>
                                    <DoubleAnimation Storyboard.TargetName="trans__TransformButton4"
                                    Storyboard.TargetProperty="X"
                                    From="0" To="100" 
                                    Duration="0:0:0.5"   BeginTime="0:0:0.3"/>

                                    <DoubleAnimation 
                                                    Storyboard.TargetName="stackPanel1"
                                                    Storyboard.TargetProperty="Opacity" 
                                                    From="0"  To="1"                                                   
                                                    Duration="0:0:1"></DoubleAnimation>

                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </Button.Triggers>
            </Button>

            <Button   
                Grid.Column="0"                
                Margin="10,50,0,0"
                Width="40" Height="40" Panel.ZIndex="3"
                HorizontalContentAlignment="Center" 
                VerticalContentAlignment="Center"
                Padding="1,1,1,1" VerticalAlignment="Top"  
                Content="&#x78;" 
                Background="{x:Null}"
                FontFamily="Wingdings"
                FontSize="30" 
                BorderBrush="{x:Null}"
                FontWeight="Bold" HorizontalAlignment="Left">
                <Button.Triggers>
                    <EventTrigger RoutedEvent="Button.Click">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard >
                                    <DoubleAnimation Storyboard.TargetName="trans__TransStackPanel"
                                    Storyboard.TargetProperty="X"
                                    From="100" To="0" 
                                    Duration="0:0:0.5" />

                                    <DoubleAnimation Storyboard.TargetName="trans__TransformGrid"
                                    Storyboard.TargetProperty="X"
                                    From="100" To="0" 
                                    Duration="0:0:0.5" />
                                    <DoubleAnimation Storyboard.TargetName="trans__TransformGrid"
                                    Storyboard.TargetProperty="Y"
                                    From="45" To="0" 
                                    Duration="0:0:0.5" />

                                    <DoubleAnimation Storyboard.TargetName="trans__TransformButton1"
                                    Storyboard.TargetProperty="X"
                                    From="100" To="0" 
                                    Duration="0:0:0.5" />
                                    <DoubleAnimation Storyboard.TargetName="trans__TransformButton2"
                                    Storyboard.TargetProperty="X"
                                    From="100" To="0" 
                                    Duration="0:0:0.5"   />
                                    <DoubleAnimation Storyboard.TargetName="trans__TransformButton3"
                                    Storyboard.TargetProperty="X"
                                    From="100" To="0" 
                                    Duration="0:0:0.5"  />
                                    <DoubleAnimation Storyboard.TargetName="trans__TransformButton4"
                                    Storyboard.TargetProperty="X"
                                    From="100" To="0" 
                                    Duration="0:0:0.5"  />

                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </Button.Triggers>
            </Button>

        </Grid>
        <StackPanel Orientation="Horizontal" 
                    VerticalAlignment="Top" Height="50" 
                    HorizontalAlignment="Right" Margin="3,0" Panel.ZIndex="5">
            <Button Width="40" Height="40"
                    Margin="5,3,5,0" VerticalAlignment="Top"
                    HorizontalAlignment="Left" 
                    Content="&#xDD;" 
                    Background="{x:Null}"
                    FontFamily="Wingdings" 
                    FontSize="26" 
                    BorderBrush="{x:Null}">
                <Button.Triggers>
                    <EventTrigger RoutedEvent="Button.Click">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard >
                                    <DoubleAnimation Storyboard.TargetName="trans__TransformGrid_"
                                    Storyboard.TargetProperty="Y"
                                    From="369" To="0" 
                                    Duration="0:0:0.2" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </Button.Triggers>
            </Button>
            <Button Width="40" Height="40" 
                    Margin="5,3,5,0" VerticalAlignment="Top" 
                    HorizontalAlignment="Left"
                    Background="{x:Null}" 
                    Content="&#xDE;"                     
                    FontFamily="Wingdings" 
                    FontSize="26"
                    BorderBrush="{x:Null}">
                <Button.Triggers>
                    <EventTrigger RoutedEvent="Button.Click">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard >
                                    <DoubleAnimation Storyboard.TargetName="trans__TransformGrid_"
                                    Storyboard.TargetProperty="Y"
                                    From="120" To="369"
                                    Duration="0:0:0.2"  />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </Button.Triggers>

            </Button>
        </StackPanel>
        <Grid x:Name="grid3"
              Margin="0,0,0,0" 
              Background="#64E895" >
            <Grid.RenderTransform>
                <TranslateTransform x:Name="trans__TransformGrid_" 
                                    X="0" Y="369" />
            </Grid.RenderTransform>
            <Label x:Name="label" Content="Settings" 
                   HorizontalAlignment="Left" 
                   Height="43" Margin="10,10,0,0" 
                   VerticalAlignment="Top" Width="100"
                   FontSize="22" FontWeight="Bold"/>
        </Grid>
    </Grid>

</Window>

8/01/2018

WPF Сustom button with animation

In this example, we'll look how to create a WPF button with the animation effect when clicked on it.
















We will write all the code in the XAML code. 
With this we use the Style object and place it in Window.Resources.
Create the Grid element and add 1 column and 1 row inside.
Then add the Grid.Background fill and use the RadialGradientBrush object to paint the area using a radial gradient.
To create this gradient, you need to add 3 GradientStop objects.
To the button, we will tie EventTrigger object RoutedEvent = "MouseDown" which will add an animation. 
We will use the DoubleAnimation object in which we will change the Background.RadiusX and Background.RadiusY property from 0 to 20.

Also, we can activate the function IsEnabled in the button.  
To do this, use Trigger Property = "IsEnabled" Value = "True".
 
<Window.Resources>
        <Style x:Key="mnuButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid x:Name="grid" Margin="0"
                                VerticalAlignment="Stretch"
                                HorizontalAlignment="Stretch"
                                Width="Auto" MinWidth="0" MinHeight="0"
                                MaxWidth="Infinity" 
                                ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                                ScrollViewer.VerticalScrollBarVisibility="Auto" >
                            <Grid.Background>
                                <RadialGradientBrush RadiusX="0" RadiusY="0" GradientOrigin="0.5,0.5">
                                    <GradientStop x:Name="gradientStop0" Color="#FF1259D6" Offset="1" />
                                    <GradientStop x:Name="gradientStop1" Color="#FFA7D3EC" Offset="0.3" />
                                    <GradientStop x:Name="gradientStop2" Color="#FF1259D6" Offset="0" />
                                </RadialGradientBrush>
                            </Grid.Background>

                            <Grid.RowDefinitions>
                                <RowDefinition ></RowDefinition>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition  ></ColumnDefinition>
                            </Grid.ColumnDefinitions>

                            <Grid.Triggers>
                                <EventTrigger RoutedEvent="MouseDown">
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation 
                                                Storyboard.TargetName="grid" 
                                                Storyboard.TargetProperty="Background.RadiusX"
                                                From="0" To="20" Duration="0:0:1.3" FillBehavior="Stop"/>
                                            <DoubleAnimation 
                                                Storyboard.TargetName="grid" 
                                                Storyboard.TargetProperty="Background.RadiusY"
                                                From="0" To="20" Duration="0:0:1.3"   FillBehavior="Stop" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </Grid.Triggers>

                            <TextBlock x:Name="textBlock" 
                                       HorizontalAlignment="Center"
                                       Margin="0" TextWrapping="Wrap"
                                       Text="TextExample"
                                       VerticalAlignment="Center"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="True">
                                <Setter Property="IsEnabled" TargetName="grid" Value="True"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
            </Style.Triggers>
        </Style>
</Window.Resources>

In order to use this style we will tie it to the real buttons in the program.

<Grid>
        <Button x:Name="button" 
                HorizontalAlignment="Left"
                Height="95" Margin="76,39,0,0"
                VerticalAlignment="Top" Width="132" 
                Style="{DynamicResource mnuButtonStyle}"/>

        <Button x:Name="button1" 
                Height="95" Margin="309,39,76,0"
                VerticalAlignment="Top" 
                Style="{DynamicResource mnuButtonStyle}"/>

        <Button x:Name="button2" 
                HorizontalAlignment="Left"
                Height="95" Margin="191,0,0,52"
                VerticalAlignment="Bottom" Width="132" 
                Style="{DynamicResource mnuButtonStyle}"/>
</Grid>