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:
            return !Validation.GetHasError(obj) &&

        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;


private void saveButton_Click(object sender, RoutedEventArgs e)

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



Wednesday, November 23, 2016

Center the window in WPF

Center the window in WPF

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


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

Maximize the window in WPF

Maximize the window in WPF

<Window WindowState="Maximized">

Disable edit function in DataGrid in WPF

<DataGrid IsReadOnly="True">

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">

Monday, November 21, 2016

Could not load file or assembly 'Microsoft.ReportViewer.Common, Version=

Could not load file or assembly 'Microsoft.ReportViewer.Common, Version=

Install the follow two componments:

- SQL Server System CLR Types for SQL Server 2014


Friday, November 18, 2016

Change WPF ComboBox dropdown width in C#

 <ComboBox Width="50" Height="40" MaxDropDownHeight="160">
            <Style TargetType="ComboBoxItem">
                <Setter Property="Width" Value="60"/>
        <ComboBoxItem Content="this is Item One "/>
        <ComboBoxItem Content="this is Item "/>
        <ComboBoxItem Content="this is "/>
        <ComboBoxItem Content="this "/>

Wednesday, November 16, 2016

RDLC reports could not be find in the Visual Studio designer

I solved by doing the following steps.

1. Open Control Panel > Programs > Programs and Features, and select the entry for your version of Microsoft Visual Studio 2015. In our case, it was Microsoft Visual Studio Enterprise 2015.

2. Click the "Change" button on the top bar above the program list.

3. After the splash screen, a window will open. Press the "Modify" button.

4. Select Windows and Web Development > Microsoft SQL Server Data Tools, and check the box next to it.

5. Press the "Update" button on the lower-right hand side of the window.

6. Once the installation is complete, open your version of Visual Studio. After the new .dll files are loaded, Reporting functionality should be reimplemented, and you should be able to access all related forms, controls, and objects.


ComboBox is extremely slow issue

Use VirtualizingStackPanel to improve performance.

<ComboBox Name="test" ItemsSource="{Binding}">
            <VirtualizingStackPanel />

Remove items from List

using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;

namespace WpfApplication1
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()

            List<Dealer> myList = new List<Dealer> {
                new Dealer() {Text = "Dealer A", IsSelected = false },
                new Dealer() {Text = "Dealer B", IsSelected = true },
                new Dealer() {Text = "Dealer C", IsSelected = false },

            myList.RemoveAll(x => x.IsSelected == true);

            foreach (Dealer d in myList)
                Debug.WriteLine("ITem: " + d.Text);

    class Dealer
        public string Text { get; set; }
        public bool IsSelected { get; set; }

Saturday, November 12, 2016

Convert comma separated string of ints to int array

Convert comma separated string of ints to int array

string sNumbers = "1,2,3,4,5";

If you want to throw an exception if a number couldn't be parsed, you can do it much more simply using LINQ:

(str ?? "").Split(',').Select<string, int>(int.Parse);

If you don't want to have the current error handling behaviour, it's really easy:

text.Split(',').Select(x => int.Parse(x));

return a list instead of int array:

var numbers = sNumbers.Split(',').Select(Int32.Parse).ToList();

List<int> numbers = new List<int>( Array.ConvertAll(sNumbers.Split(','), int.Parse) );

// Uses Linq
var numbers = Array.ConvertAll(sNumbers.Split(','), int.Parse).ToList();