Simple life, Complicated mind

Saturday, December 3, 2016

LINQ LIMIT

LINQ LIMIT

optArr.Where(d => d.IsSelected == true).Select(d => d.ID).Skip(0).Take(1).FirstOrDefault();

Friday, December 2, 2016

listen to MouseOver event

listen to MouseOver event

public MainWindow()
{
    InitializeComponent();

    StackPanel stackpanel = new StackPanel(); 
    stackpanel.MouseEnter += new MouseEventHandler(stackpanel_MouseEnter);
    stackpanel.MouseLeave += new MouseEventHandler(stackpanel_MouseLeave);
}

void stackpanel_MouseLeave(object sender, MouseEventArgs e)
{
    StackPanel stackpanel = (StackPanel)sender;
    stackpanel.Background = Brushes.Transparent;
}

void stackpanel_MouseEnter(object sender, MouseEventArgs e)
{
    StackPanel stackpanel = (StackPanel)sender;
    stackpanel.Background = Brushes.LightGray;
}

Reference:

http://stackoverflow.com/questions/10184551/stackpanel-highlight-on-mouse-over-from-code-behind

Wednesday, November 30, 2016

Detecting WPF controls' Validation Errors when saving

Detecting WPF Validation Errors

using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace erpcli.Lib
{
    static class Validator
    {
        // LINQ version
        public static bool IsValid(DependencyObject obj)
        {
            // The dependency object is valid if it has no errors and all
            // of its children (that are dependency objects) are error-free.
            // Reference: http://stackoverflow.com/questions/127477/detecting-wpf-validation-errors
            return !Validation.GetHasError(obj) &&
            LogicalTreeHelper.GetChildren(obj)
            .OfType<DependencyObject>()
            .All(IsValid);
        }

        /*
        public static bool IsValid(DependencyObject parent)
        {
            if (Validation.GetHasError(parent))
                return false;

            // Validate all the bindings on the children
            for (int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); ++i)
            {
                DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                if (!IsValid(child)) { return false; }
            }

            return true;
        }
        */
    }
}

Usage:

private void saveButton_Click(object sender, RoutedEventArgs e)
{

  if (Validator.IsValid(this.MyStackPanel)) // is valid
   {

    ....
   }
}

Reference:

http://stackoverflow.com/questions/127477/detecting-wpf-validation-errors

Wednesday, November 23, 2016

Center the window in WPF

Center the window in WPF

<Window WindowStartupLocation="CenterScreen"></Window>

Or

<Window WindowStartupLocation="CenterOwner"></Window>

Maximize the window in WPF

Maximize the window in WPF

<Window WindowState="Maximized">
</Window>

Disable edit function in DataGrid in WPF

<DataGrid IsReadOnly="True">
</DataGrid>

Prevent mutiple row selection (select single row) in WPF DataGrid

Prevent mutiple row selection (select single row) in WPF DataGrid

<DataGrid SelectionMode="Single" SelectionUnit="FullRow">
</DataGrid>