Showing posts with label windows. Show all posts
Showing posts with label windows. Show all posts

Jan 4, 2018

WPF Error 40 BindingExpression path error: property not found on 'object'

On XAML, I created a datatemplate with binding for an object called Skills.
And I set the datatemplate in my C# program on a ListViewItem like this:

DataTemplate template = (DataTemplate)this.FindResource("SkillListItem");
ListViewItem litem = new ListViewItem();
litem.ContentTemplate = template; 

And on visual studio I got this error:
WPF Error 40 BindingExpression path error: 'Name 'property not found on 'object' Skill

It is a strange error that can solve by changing
public string Name;
 
to this
public string Name{ get; set; }


 

Dec 27, 2017

How to Find Out Which Build and Version of Windows 10 You Have

The new Settings app also offers build, edition, and version information in a user-friendly form. Hit Windows+I to open Settings. In the Settings window, navigate to System > About.  Scroll down a bit and you’ll see the information you’re after.


Dec 17, 2017

A Better Way to Data Bind Enums in WPF

Have you needed to data bind enums in WPF? Well, if you write WPF applications, you bet your ass you have! In this post, I would like to show you how I like to deal with enums in WPF. Fo this post, I will be using the following enum to bind to.

public enum Status
{
    Horrible,
    Bad,
    SoSo,
    Good,
    Better,
    Best
}

The WPF Way

Normally if you were going to data bind a control to your enum you would need to go through a number of steps.  First you need to add a XAML namespace for your local enum type and to System in MSCorLib.

xmlns:local="clr-namespace:BindingEnums"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Then you would to create an ObjectDataProvider as a resource.  Give it an x:Key so you can use it in your code, set the MethodName to “GetValues” which exists on the enum data type, and then set the ObjectType to that of the enum type.  But wait, you’re not done yet.  Next you need to set the ObjectDataProvider.MethodParameters to that of your enum type.  So you will end up with something like this.

<Window.Resources>
    <ObjectDataProvider x:Key="dataFromEnum" MethodName="GetValues"
                        ObjectType="{x:Type sys:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:Status"/>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>
Now, you you have something you can data bind to.  For example, if I wanted to data bind a ComboBox to this enum, I would have to set it’s ItemsSource to a new Binding, and set the source to that of our enum ObjectDataProvider we created above.  Like so:

<Grid>
    <ComboBox HorizontalAlignment="Center" VerticalAlignment="Center" MinWidth="150"
              ItemsSource="{Binding Source={StaticResource dataFromEnum}}"/>
</Grid>
And viola! Our enum is data bound.

This isn’t bad.  It works just fine, but it requires a lot more code than I would like to have to write over and over.  I would rather write the code once, and just change the enum type so I can reuse all that logic all over my application.  With the above approach we have to create a new ObjectDataProvider for each enum we have, which just means more code to maintain, and more chances of writing broken XAML.  Especially since you are more likely to copy and paste a previous ObjectDataProvider you created and just change some parameters.  Hence, it’s more error prone.

The Better Way

Let’s look at how we can use features of WPF to improve the usage and readability of our code when data binding to enums.  First, I want to encapsulate all that logic for creating a bindable list of enum values without the need of an ObjectDataProvider.  I also want to eliminate the need to create a Resource that must be defined in order to be used in my XAML.  Ideally I would just do everything inline like I would with a normal object binding.  To accomplish this, I am going to enlist the help of a custom MarkupExtension.  This extension will simply take an enum Type and then create a bindable list of enum values for my control.  It’s actually quite simple.  Take a look:

public class EnumBindingSourceExtension : MarkupExtension
{
    private Type _enumType;
    public Type EnumType
    {
        get { return this._enumType; }
        set
        {
            if (value != this._enumType)
            {
                if (null != value)
                {
                    Type enumType = Nullable.GetUnderlyingType(value) ?? value;                     if (!enumType.IsEnum)
                        throw new ArgumentException("Type must be for an Enum.");
                }
                this._enumType = value;
            }
        }
    }
    public EnumBindingSourceExtension() { }
    public EnumBindingSourceExtension(Type enumType)
    {
        this.EnumType = enumType;
    }
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (null == this._enumType)
            throw new InvalidOperationException("The EnumType must be specified.");
        Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType;
        Array enumValues = Enum.GetValues(actualEnumType);
        if (actualEnumType == this._enumType)
            return enumValues;
        Array tempArray = Array.CreateInstance(actualEnumType, enumValues.Length + 1);
        enumValues.CopyTo(tempArray, 1);
        return tempArray;
    }
}

Now we can simplify our XAML and simply create an enum binding like this:
<Grid>
    <ComboBox HorizontalAlignment="Center" VerticalAlignment="Center" MinWidth="150"
              ItemsSource="{Binding Source={local:EnumBindingSource {x:Type local:Status}}}"/>
</Grid>
Same result, but without the need for an ObjectDataProvider.

Adding Description Support

Now that we have our enum binding working perfectly without the need for that silly ObjectDataProvider, we can improve our enum experience.  It is very common to have a enum to represent the values, while at the same time, give the enum value a description so that it is more readable to your user.  For example; let’s say you have an enum will all the state abbreviations (ID, TX, AZ, etc.), but you would much rather display the full state name (Texas, Idaho, Arizona, etc.).  Wouldn’t it be nice to have this ability?  Well, luckily for us, this is easy to.  We will just need to use a simple TypeConverter that we can attribute our enum with.  Check it out:

public class EnumDescriptionTypeConverter : EnumConverter
{
    public EnumDescriptionTypeConverter(Type type)
        : base(type)
    {
    }     public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string))
        {
            if (value != null)
            {
                FieldInfo fi = value.GetType().GetField(value.ToString());
                if (fi != null)
                {
                    var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    return ((attributes.Length > 0) && (!String.IsNullOrEmpty(attributes[0].Description))) ? attributes[0].Description : value.ToString();
                }
            }
            return string.Empty;
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

Now add the attribute and add some descriptions to our enum:
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum Status
{
    [Description("This is horrible")]
    Horrible,
    [Description("This is bad")]
    Bad,
    [Description("This is so so")]
    SoSo,
    [Description("This is good")]
    Good,
    [Description("This is better")]
    Better,
    [Description("This is best")]
    Best
}
BAM!  We now have our descriptions display to our users, but the actual values remain intact.

That does it for this post.  Hopefully you will find this useful, and maybe even use this approach in your WPF applications.  Be sure to check out the source code, and start playing with it.  As always, feel free contact me on my blog, connect with me on Twitter (@brianlagunas), or leave a comment below for any questions or comments you may have.

REFERENCE: http://brianlagunas.com/a-better-way-to-data-bind-enums-in-wpf/
 

Dec 7, 2017

"There are no TAP-Windows adapters on this system" after updating Windows 10

If you can't connect to VPN after a cliche Windows Update, Just Uninstall VPN Tool (such as OpenVPN, Sophos etc.) andt install it. It will work, Thank you Windows for providing a traditional bug and a solution for us!

Example Error from VPN Client:

Thu Dec 07 20:59:03 2017 MANAGEMENT: Client disconnected
Thu Dec 07 20:59:03 2017 There are no TAP-Windows adapters on this system.  You should be able to create a TAP-Windows adapter by going to Start -> All Programs -> TAP-Windows -> Utilities -> Add a new TAP-Windows virtual ethernet adapter.
Thu Dec 07 20:59:03 2017 Exiting due to fatal error

Nov 22, 2017

Clean Unity’s Project Wizard in Unity 5.x on Windows 10

There are two ways to delete a project in the Unity’s project wizard. The first one is simply deleting, renaming or moving the directory directly from its location. The project wizard is smart enough and this is the recommended option.


The second one requires administrator privileges and deleting the registry entries associated with the projects we want to get rid of from the wizard’s list without deleting, moving or renaming the directories.
1 - Go to Windows registry (run regedit.exe).

2 - In the Windows registry, go to
HKEY_CURRENT_USER/Software/Unity Technologies/Unity Editor 5.x/

4- Find the entries which names begin with “RecentlyUsedProjectPaths-“. They’re numbered. If you click on the entry you can see your project path.

5- Select and delete the entries you want to get rid of.

Reference: This post is updated version of the solution from http://jorge.palacios.co/clean-unitys-project-wizard-in-unity-4-x/ 

May 3, 2012

the user does not have sufficient privileges to install Adobe Flash Player (Windows XPXP)

ERROR While installing Flash Player:


the user does not have sufficient privileges to install Adobe Flash Player

1- First uninstall Flash Player with this tool: http://helpx.adobe.com/flash-player/kb/uninstall-flash-player-windows.html


2- Then install the latest flash release with your administrator account : http://www.adobe.com/support/flashplayer/downloads.html

more info about Flash Player 11 (Stage3D, molehill etc.): http://www.adobe.com/tr/products/flashplayer.html



May 23, 2011

Oct 17, 2010

Deleting Windows.old files (Vista)

1- Go to Start->All Programs>Accessories> System Tools -> Disk Cleanup

2- Then system will search for useless files.

3- Choose previous windows files and make the system erase them.


Sep 1, 2010

Can't see hidden files on Windows even if changed in the Control Panel

1. Go to Start --> Run, then type Regedit
2. Navigate to the registry folder HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\
CurrentVersion\Explorer\Advanced\Folder\Hidden\SHO WALL
3. Find a key called CheckedValue.
4. Double Click CheckedValue key and modify it to 1. This is to show all the hidden files.

Jul 24, 2010

Way to get banned Google services and Youtube

1- In Windows, edit C:\WINDOWS\system32\drivers\etc\hosts
2- In Linux edit /etc/hosts

3- Enter:

74.125.43.103 code.google.com
74.125.43.103 pages.google.com
74.125.43.103 video.google.com
74.125.43.103 translate.google.com
74.125.43.103 translate.google.com.tr
74.125.43.103 docs.google.com
74.125.43.103 sites.google.com
74.125.43.103 books.google.com
74.125.43.103 chrome.google.com
74.125.43.103 sketchup.google.com
74.125.43.103 froogle.google.com
74.125.43.103 labs.google.com
74.125.43.103 mars.google.com
74.125.43.103 moon.google.com
74.125.43.103 notebook.google.com
74.125.43.103 toolbar.google.com
74.125.43.103 browsersync.google.com
74.125.43.103 catalog.google.com
74.125.43.103 codesearch.google.com
74.125.43.103 dir.google.com
74.125.43.103 earth.google.com
74.125.43.103 groups.google.com.tr
74.125.43.103 shopping.google.com
74.125.43.103 sky.google.com
74.125.43.103 support.google.com
74.125.43.103 tools.google.com
74.125.43.103 wap.google.com
74.125.43.103 answers.google.com
74.125.43.103 google-analystics.com
74.125.43.103 www.google-analystics.com
74.125.43.103 youtube.com
74.125.43.103 www.youtube.com
74.125.43.103 clients1.google.com.tr
74.125.43.103 lh1.ggpht.com
74.125.43.103 lh2.ggpht.com
74.125.43.103 lh3.ggpht.com
74.125.43.103 lh4.ggpht.com
74.125.43.103 lh5.ggpht.com
74.125.43.103 lh6.ggpht.com
74.125.43.103 dl-ssl.google.com
74.125.43.103 s.ytimg.com
74.125.43.103 dl.google.com
74.125.43.103 ssl.google.com

4- Restart networking, here you go :)

Visual Studio Keyboard Shortcuts

Playing with keyboard shortcuts is very interesting and reduce the headache of using the mouse again and again while programming with visu...