Grid in WPF Page

There is a difference behavior of Grid resided in Window and Page?

There is a difference behavior of Grid resided in Window and Page?

Xaml code for window

<Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
    &lt;Grid Grid.Row="1" ShowGridLines="True"&gt;
        &lt;Grid.RowDefinitions&gt;
            &lt;RowDefinition Height="*"/&gt;
            &lt;RowDefinition Height="*"/&gt;
            &lt;RowDefinition Height="*"/&gt;
        &lt;/Grid.RowDefinitions&gt;
        &lt;Grid.ColumnDefinitions&gt;
            &lt;ColumnDefinition Width="*"/&gt;
            &lt;ColumnDefinition Width="*"/&gt;
        &lt;/Grid.ColumnDefinitions&gt;
        &lt;Button Grid.Column="1"&gt;

        &lt;/Button&gt;
        &lt;Button Grid.Column="0"&gt;

        &lt;/Button&gt;
        &lt;Label Grid.Row="1" Width="50"&gt;

        &lt;/Label&gt;
        &lt;DatePicker Grid.Column="0" Grid.Row="2"&gt;

        &lt;/DatePicker&gt;
    &lt;/Grid&gt;
&lt;/Grid&gt;

Xaml code for Page

 <Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>

    &lt;Grid Grid.Row="1" ShowGridLines="True"&gt;
        &lt;Grid.RowDefinitions&gt;
            &lt;RowDefinition Height="*"/&gt;
            &lt;RowDefinition Height="*"/&gt;
            &lt;RowDefinition Height="*"/&gt;
        &lt;/Grid.RowDefinitions&gt;
        &lt;Grid.ColumnDefinitions&gt;
            &lt;ColumnDefinition Width="*"/&gt;
            &lt;ColumnDefinition Width="*"/&gt;
        &lt;/Grid.ColumnDefinitions&gt;
        &lt;Button Grid.Column="1"&gt;

        &lt;/Button&gt;
        &lt;Button Grid.Column="0"&gt;

        &lt;/Button&gt;
        &lt;Label Grid.Row="1" Width="50"&gt;

        &lt;/Label&gt;
        &lt;DatePicker Grid.Column="0" Grid.Row="2"&gt;

        &lt;/DatePicker&gt;
    &lt;/Grid&gt;
&lt;/Grid&gt;

Both the same code, but I am unable to position controls inside the grid column and row in wpf Page. In wpf window it work as intended. What am i doing wrong?

WPF and WinForms Will Run on .NET Core 3

WPF and WinForms Will Run on .NET Core 3

If you haven't had a chance to explore the new capabilities of .NET Core 3, read along with an expert in the framework to discover what it brings to the table.


Maybe you already heard or read about the fact that Microsoft brought WinForms and WPF to .NET Core 3.0. Maybe you already saw the presentations on the Connect conference, or any other conference or recording when Scott Hanselman shows how to run a pretty old WPF application on .NET Core. I saw a demo where he ran BabySmash on .NET Core.

BTW: My oldest son really loved that BabySmash when he was a baby.
WPF and WinForms on .NET Core?

I was really wondering about this step, even because I wrote an article for a German .NET magazine some months before where I mentioned that Microsoft won’t build a UI Stack for .NET Core. There were some other UI stacks built by the community. The most popular is Avalonia.

But this step makes sense anyway. Since the .NET Standards move the API of .NET Core more to the same level of the .NET framework, making the APIs almost equal was simply a question of time. WPF and WinForms are based on .NET Libraries, so it should run on .NET Core.


Does This Mean it Runs on Linux and Mac?

Nope! Since WinForms and WPF uses Windows only technology in the background, it cannot run on Linux or Mac. It is really dependent on Windows. The sense of running it on .NET Core is performance and to be independent of any framework. .NET Core is optimized for performance so you can run super fast web applications in the cloud. .NET Core is also independent of the installed framework on the machine. Just deploy the runtime together with your application.

You are now able to run fast and self-contained Windows desktop applications. That’s awesome, isn’t it!?


The .NET CLI

Every time I install a new version of the .NET Core runtime I try dotnet new and I was positively shocked about what I saw this time:

You are now able to create a Windows Form or a WPF application using the .NET CLI. This is cool. And I for sure needed to try it out:

dotnet new -n WpfTest -o WpfTest dotnet new -n WpfTest -o WpfTest 

And yes, it is working as you can see here in Visual Studio Code:

And this is the WinForms project in VS Code

Running dotnet run on the WPF project:

And again on the WinForms GUI:


IDE

Visual Studio Code isn’t the right editor for this kind of project. If you know XAML pretty well, it will work, but WinForms definitely won’t work well. You need to write the designer code manually and there isn’t any designer support yet. Maybe there will be some in the future, but I’m not sure.

The best choice to work with WinForms and WPF on .NET Core is Visual Studio 2017 or newer.


Last Words

I don’t think I will now start to write desktop apps on .NET Core 3, because I’m a web guy. But it is a really nice option to build apps like this on .NET Core.

BTW: Even EF 6 will work in .NET Core 3, which means you also don’t need to rewrite the database access part of your desktop application.

As I wrote, you can now use this super fast framework and the option to create self-contained apps. I would suggest you try it out and play around with it. Do you have an older desktop application based on WPF or WinForms? I would be curious about whether you can run it on .NET Core 3. Tell me how easy it was to get it running on .NET Core 3.


Learn More

The Complete ASP.NET MVC 5 Course

Build a Real-world App with ASP.NET Core and Angular 2 (4+)

ASP NET Core (ASP.NET 5),MVC 6,C#,Angular2 & EF Crash Course

Rest Api’s in Asp.Net and C#

Hands on ASP .Net Core 2

Originally published by Juergen Gutsch at https://dzone.com

WPF “not a valid value for property” error for attached property even though it works as expected

I have a TextBox with an attached property on it, and Visual Studio gives me an error saying that the property value is not valid, even though it is in fact valid.

I have a TextBox with an attached property on it, and Visual Studio gives me an error saying that the property value is not valid, even though it is in fact valid.

Here's the TextBox:

<TextBox 
    HorizontalAlignment="Stretch" 
    VerticalAlignment="Top" 
    Width="Auto" 
    styleExtensions:TextBoxExtension.BorderStyle="None"/>

And this is the AttachedProperty:

public enum BorderStyle
{
    Rounded,
    BottomOnly,
    None
}

public static DependencyProperty BorderStyleProperty =
DependencyProperty.RegisterAttached(
"BorderStyle",
typeof(BorderStyle),
typeof(TextBoxExtension));

The GetBorderStyle and SetBorderStyle methods are also present, and since they do nothing special, I didn't paste them here.

When I rut it, the program works as expected, all three values for BorderStyle do what they're supposed to (they trigger different changes in a Style) and even the visual xaml editor shows the changes as expected, but for some reason Visual Studio keeps giving me this pointless error for this particular TextBox:

'None' is not a valid value for property 'BorderStyle'

The error persists for all three values of the enum. The same attached property is also used somewhere else in a ControlTemplate, on a different TextBox and it doesn't trigger this error.

I've tried cleaning the solution, rebuilding it, deleting the bin and obj folders, deleting the .vs folder, but the error keeps showing up.

Am I doing something wrong here, or is Visual Studio wrong? And if so, is there a workaround to disable this specific error message?

I'm using Visual Studio 2017 15.8.8, and .NET Framework 4.7.03056

Can't set properly my Dialog Window size and placement target

I've built a dialog window that i plan to use in my whole application instead of using a message box. Whenever i need to use it, i would call it in the code behind of my Window i'm currently working with this syntax:

I've built a dialog window that i plan to use in my whole application instead of using a message box. Whenever i need to use it, i would call it in the code behind of my Window i'm currently working with this syntax:

public void ShowDialogWindow(object sender, DialogEventArgs e)
{
    DialogWindow dialog = new DialogWindow(e.MessageToShow, DialogType.Error, ButtonsType.OkOnly, this.ActualWidth, this.ActualHeight, this);
    dialog.ShowDialog();
}

this is the constructor of my Dialog Window

public DialogWindow(string messageToDisplay, DialogType dialog, ButtonsType buttons, double width, double height, object Owner)
{
    InitializeComponent();
    this.DataContext = this;
    this.Owner = Owner as Window;
    AWidth = width;
    AHeight = height;
    -----
}

and this is the opening Window tag in the xaml

<Window x:Class="DialogWindow"
        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"
        ---
        mc:Ignorable="d" WindowStyle="None" 
        WindowStartupLocation="CenterOwner"
        AllowsTransparency="True" 
        Width="{Binding AWidth}" Height="{Binding AHeight}" 
        MinHeight="720" MinWidth="1080">

Now my problem is. When i call this dialog when the owner is Minimized (having set MinWidth = 1080 and MinHeght = 720) the dialog "kinda" fits (ActualWidth and Actual Height of both Window and DialogWindow are the same, but visually the DialogWindow seems a little bit bigger than the Owner)

But when i go full screen it happens this:

Not only the ActualHeight is different to the property AHeight (which is set correctly to the ActualHeight of the Owner), but also is not centered on the Owner window at all, but overflow on my second screen. What cause this, and how can i solve it?

Saving imported excel data in a datagrid to sqlite database

I'm trying to save the data I have imported from excel into my datagrid to my database. Data base details File - tensiondata.db Table - data I can't find enough information on how to perform this, I've attached the code below. I'm not even certain that saving it to a database is the right solution. Is there a way you can save the datagrid somewhere else?

I'm trying to save the data I have imported from excel into my datagrid to my database. Data base details File - tensiondata.db Table - data I can't find enough information on how to perform this, I've attached the code below. I'm not even certain that saving it to a database is the right solution. Is there a way you can save the datagrid somewhere else?

`<Window x:Class="WpfApp1.Window3"
        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:WpfApp1"
        mc:Ignorable="d"
Title="Open Excel File for Tension, Velocity or Takeup" Height="550" Width="900"&gt;
&lt;Grid&gt;
    &lt;Image Source="Capture.jpg" VerticalAlignment="Top" Height="68" Margin="5,10,0,0" HorizontalAlignment="Left" Width="75"/&gt;


    &lt;GroupBox x:Name="Options" Header="Options" HorizontalAlignment="Left" Height="62" Margin="85,8,0,0" VerticalAlignment="Top" Width="562"&gt;

    &lt;/GroupBox&gt;

    &lt;Button x:Name="save_file" Content="Save and Update" HorizontalAlignment="Left" Margin="451,22,0,0" VerticalAlignment="Top" Width="176" Height="40" Click="Button_Click_1" FontSize="16" FontWeight="Bold"/&gt;
    &lt;Button Content="Generate Graph" FontWeight="Bold" HorizontalAlignment="Left" Margin="350,463,0,0" VerticalAlignment="Top" Width="164" Height="29" Click="Button_Click"/&gt;
    &lt;TextBox x:Name="txtFilePath" HorizontalAlignment="Left" Height="24" Margin="189,32,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233"/&gt;
    &lt;Button Content="Search" FontWeight="Bold" HorizontalAlignment="Left" Margin="100,32,0,0" VerticalAlignment="Top" Width="78" Height="24" x:Name="search_file" Click="Open_Click"/&gt;
    &lt;DataGrid AutoGenerateColumns="True" IsReadOnly="False" HorizontalAlignment="Center" Name="dtGrid" VerticalAlignment="Center" Height="356" Margin="37,83,44,80" Width="811" RenderTransformOrigin="0.529,0.503" /&gt;


&lt;/Grid&gt;

</Window>

public partial class Window3 : Window

{
    string dbConnectionString = @"Data Source = tensiondata.db;Version=3;"; // String connection 
    private object con;

    public Window3()
    {
        InitializeComponent();
    }

    private void Open_Click(object sender, RoutedEventArgs e) // Opening Excel file into data grid 
    {
        OpenFileDialog openfile = new OpenFileDialog();
        openfile.DefaultExt = ".xlsx";
        openfile.Filter = "(.xlsx)|*.xlsx";
        //openfile.ShowDialog();

        var browsefile = openfile.ShowDialog();

        if (browsefile == true)
        {
            txtFilePath.Text = openfile.FileName;

            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(txtFilePath.Text.ToString(), 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Worksheets.get_Item(1); ;
            Microsoft.Office.Interop.Excel.Range excelRange = excelSheet.UsedRange;

            string strCellData = "";
            double douCellData;
            int rowCnt = 0;
            int colCnt = 0;

            DataTable dt = new DataTable();
            for (colCnt = 1; colCnt &lt;= excelRange.Columns.Count; colCnt++)
            {
                string strColumn = "";
                strColumn = (string)(excelRange.Cells[1, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
                dt.Columns.Add(strColumn, typeof(string));
            }

            for (rowCnt = 1; rowCnt &lt;= excelRange.Rows.Count; rowCnt++)
            {
                string strData = "";
                for (colCnt = 1; colCnt &lt;= excelRange.Columns.Count; colCnt++)
                {
                    try
                    {
                        strCellData = (string)(excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
                        strData += strCellData + "|";
                    }
                    catch (Exception ex)
                    {
                        douCellData = (excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
                        strData += douCellData.ToString() + "|";
                    }
                }
                strData = strData.Remove(strData.Length - 1, 1);
                dt.Rows.Add(strData.Split('|'));
            }

            dtGrid.ItemsSource = dt.DefaultView;


            excelBook.Close(true, null, null);
            excelApp.Quit();
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e) // Next Window Button 
    {
        Window2 sec = new Window2();
        sec.ShowDialog();
    }



    private void Button_Click_1(object sender, RoutedEventArgs e) // Save and update button
    {
        //Open connection to database
        SQLiteConnection SQLiteCon = new SQLiteConnection(dbConnectionString);
        SQLiteCon.Open();

        try
        {


        }

        //SQLiteCon.Close();
        //}

        // while (dr.Read())
        // {


        //  }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }


    }
}

}`


WPF Path disappears at some size

I have encountered this problem while scaling graph, which is drawn over GIS control Greatmap. But a simple experiment persuades me that problems is somewhere deeper in WPF.

I have encountered this problem while scaling graph, which is drawn over GIS control Greatmap. But a simple experiment persuades me that problems is somewhere deeper in WPF.

Consider simple WPF application:

This is MainWindow.xaml

<Grid>
    <StackPanel>
        <Slider ValueChanged="Size_Changed" Minimum="0" Maximum="300000"/>
        <TextBox x:Name="Value"></TextBox>
    </StackPanel>
    <Canvas>
        <Path x:Name="MyPath" Stroke="Black" StrokeThickness="2" />
    </Canvas>
</Grid>

And this is its code behind

private void Size_Changed(object sender,
        RoutedPropertyChangedEventArgs<double> e)
    {
        if (MyPath == null) return;
    var g = new StreamGeometry();
    using (var s = g.Open())
    {
        var pointA = new Point(0, 200);

        s.BeginFigure(pointA, false, false);

        var pointB = new Point(e.NewValue, 200);

        s.PolyLineTo(new[] {pointB}, true, true);

        Value.Text = $"zoom = {e.NewValue:0.0} ;  pointA = {pointA.X:#,0} ; pointB = {pointB.X:#,0}";
    }

    g.Freeze();

    MyPath.Data = g;
}

While I drag slider from 0 to 249999 it’s all right. I can see line on my view. But at the very moment slider’s value becomes 250000 – line disappears.

Is there any limitations in WPF ?