Hide inactive cursor behavior.

In this article I’ll explain one way how to hide a cursor after n-seconds of inactivity though WPF behavior mechanism. Same effect you can observe on almost any video player — if you don’t touch a mouse for the few seconds, cursor will disappear.

First of all, what is a behavior?

A behavior encapsulates pieces of functionality into a reusable component, which we later on can attach to an element in a view. Emphasis is on reusable. Behaviors also have the benefit of keeping the MVVM pattern intact, since we can move code from code behind to behaviors. You can find a good introduction on the behaviors, type of behaviors in a great article of Robert Hedgate.

Implementation though attached behavior.

Advantage of attached behavior is that it can be used in a native windows store app. Disadvantage is that it does not show up in Blend under behaviors, and thus cannot be attached if you are using Blend. In my case it’s very important to have possibility of using it on windows store app.

In the implementation of the behavior we have 4 dependency properties:

  • Interval — time interval after which cursor should be hided.
  • IsEnabled — enable or disable behavior functionality.
  • ActiveCursor — cursor type when it’s not active.
  • InactiveCursor — cursor type when it’s active.

Logic isn’t really complicated so you just can download it from my Bitbucket.

Advertisements

What has been done during 2011 year?

This year is almost finished and I’d like to summarize what has been done, what I should improve. New Year’s good time to know: where you are on the road map, add new goal to life and analyse productivity.

If you want to do something grandiose in one of the beautiful days,

remember – this day is today!

(c) George Lucas

This list I had in the beginning of 2011 year (of course without my private goals which all have been done) and what has been done:

  1. Change a job.Success

    I have been working for 3 years for one company in Ukraine. It was my first job, I grown there from junior-developer to senior developer. I met a lot of interesting people; I worked with customers which gave me knowledge in variety areas. However, on someday I realize that our ways are different, that I need something more in business and technical knowledge. For this I decided to push myself a little bit. My new company was in the Donetsk, Ukraine too. I worked there around half a year as a head of .NET department but on not good productL. (Why I left that company on the next item).

  2. Immigration.Success

    In a university I made a decision to work (or live) abroad. In 2011 year I had decided to do it. As a result I’m working with high-motivated professionals for fantastic company in Antwerp city, Belgium.

  3. Visit one of the solid conferences.Success

    On November 14-15, 2011 I’ve visited a SQL Server Days 2011. If you want to know more please read this post about first day and this one about second day in a conference.

  4. Pass 70-564 and become a MCPD ASP.NET developer.Success
  5. Read 10 books.Success

    At the end of the year I read around 15-20 maybe (I calculated only technical books). So this item was underestimated. As a result on the next year I put 20 (I don’t want to improve this now because I need time for other things). Last book were about project management and Agile You can find some of them on my LinkedIn profile. And I’m planning to move on that direction. I feel that I need more knowledge on business too (Maybe for this I’ll graduate some business college or university here in Belgium. I already have one which interesting for me. It’s EHSAL Management School. So if you know something or ever graduated that one please live a comment!)

  6. Pass 70-523: upgrade MCPD ASP.NET 3.5 to MCPD ASP.NET 4.0.Fail

    It was one of things which I decided not to do and really regret about. Last year all my projects were Silverlight. I’m feeling shortage of experience on this area. However I’m not sure that I put it to the next year’s list.

  7. English: pass IELTS.Fail

    I’m absolutely sure that this one is the biggest my fault. I definitely need to improve my English. I decided not to take this but I’m 100% sure that “pass IELTS” is one of the strong motivated factors. This item is absolutely MUST HAVE on my next year’s list.

  8. Create a first Windows Phone 7 application.Fail

    I decided to move on this direction. I really like this OS and Metro style. I’m working around 3 month on my application. Now I think it’s ready on 60%-70%. I still should implement good design and publish it on marketplace. Hope, I can do it on February.

  9. Create a blog with N-thousands visitors.Success

    At the end of the year I’ve exceeded this item on 155%. However, now I think it’s my mistake. I shouldn’t evaluate blog by “visitor” measure. I need to think about this item on next year because I don’t satisfy with “post count” measure too.

In a conclusion I would like to maintain that 2011 year was really productive for me with a lot of fun and success. I hope next one would be ever more interesting and productive. Also I’d like to say thank you everyone how was on this year with me!

I hope you all enjoyed Christmas, and I wish you all a happy and productive 2012 year!

Choose control style in XAML through converter.

Frequently we need to change control style (or template) dynamic. Of course it can be done on different ways:

  • Put two controls and “play” with Visibility property and binding – I suppose it’s the less smart case;
  • You can choose DataTemplate/ControlTemplate in code behind file;
  • And use converter.

I’ll show the last one.

The task is: we have a control (in my case it’s just an expand/collapse button) and we need to choose appropriate style for this control which depends on a model.

I created two styles:

  1. True style for expanded state
     <Style TargetType="Button">
                    <Setter Property="Width" Value="13" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Grid Width="13" Height="13" VerticalAlignment="Center" HorizontalAlignment="Center" Background="Transparent" Cursor="Hand">
                                    <Rectangle StrokeThickness="1" Stroke="{Binding ForegroundColor}" RadiusX="2" RadiusY="2" />
                                    <Rectangle Fill="{Binding ForegroundColor}" Height="1" VerticalAlignment="Center" Margin="3" />
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
  1. False style for collapsed state
    <Style TargetType="Button">
                    <Setter Property="Width" Value="13" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Grid Width="13" Height="13" VerticalAlignment="Center" HorizontalAlignment="Center" Background="Transparent" Cursor="Hand">
                                    <Rectangle StrokeThickness="1" Stroke="{Binding ForegroundColor}" RadiusX="2" RadiusY="2" />
                                    <Rectangle Fill="{Binding ForegroundColor}" Height="1" VerticalAlignment="Center" Margin="3" />
                                    <Rectangle Fill="{Binding ForegroundColor}" Width="1" HorizontalAlignment="Center" Margin="3"  />
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>

Next step, I create a BoolToStyleConverter:

    public class BoolToStyleConverter : IValueConverter
    {
        public Style TrueStyle { get; set; }

        public Style FalseStyle { get; set; }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((bool)value) ? TrueStyle : FalseStyle;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

Also I create model with following properties:

    public class TaskModel
    {
        public bool IsExpanded { get; set; }

        public Brush ForegroundColor { get; set; }
    }

As a result, I’ve got following behavior only in a XAML. Additionally, I can use a bind (in my case it’s just a foreground color of the model to my style).

Expanded state

Collapsed state

Enjoy!

Silverlight: stretch ListBoxItem horizontally.

Very often you need to stretch your ListBoxItem horizontally. If the ListBox is simple (without ItemTemplate), there is easy solution: just add

HorizontalContentAlignment="Stretch"

to the ListBox.

However, in second way the solution is easy too. But frequently we spend a lot of time to fix it. There is a solution:

<ListBox.ItemContainerStyle>
<Style TargetType=”ListBoxItem”>
<Setter Property=”HorizontalContentAlignment” Value=”Stretch”></Setter>
</Style>
</ListBox.ItemContainerStyle>

Enjoy!

Dynamically create a DataTempalte in Silverlight code.

The issue is following: we have a ListBox and for some reason we need to create a ListBox.ItemTemplate in code. In my case, I had to build a ListBox were each item is a button and I don’t know the template of it. I got it from other service. So there is no choose to do it on some other case, except of whole refactoring of architecture.

First of all, I’d like to maintain: don’t do it!
J It’s not a juke, there are lots of other possibility to avoid this situation, such as: use a converters/multy-converters, use predefined templates, eventually different styles for your ListBox. However, it’s not always possible.

First way: use a XamlLoader.

Just create necessary template like a string in code behind and load it

(DataTemplate) XamlReader.Load(“<DataTemplate>…</DataTemplate>”);

Note: don’t forget about necessary namespace. But in our case we don’t know what template we have.

Second way: use a ContentControl.

I used a ContentControl (I placed here a button with a DataTemplate) and Loaded event.

In test application I just loaded a ControlTemplate for button from Resources. Then added a button with click handler:

Eventually, this button I added like a DataTemplate to ListBoxItem.

Hope it’ll help.

       I haven’t been here around 5 months L It’s really sad! But it’s not only because I’m a lazy. There were a lot of changes in my life during this time. I completely went to Silverlight/WP7 development, change a job (ever twice!) and…change a country. Now I’m working, living and drinking a Belgium beer in Antwerp, Belgium. This was agenda and now let’s does it more extended about everything.

I really like Silverlight, enjoy what you can do with it and how. Everything can be done very fast and in OOP+TDD paradigms. In other hand I completely misunderstand Microsoft and their politics with this technology (I suppose I’ll write a separate post about this one). Maybe they think that everything should be possible on Windows 8? – Foolish idea. They gave us a Visual Studio plugin for Python, Ruby and PHP, now they negotiate the html 5. Of course it’s good to have lots of possibilites, however developers and especially business don’t understand where is a future, what is a main way! Ok, it’s a long story which I promise to tell soon.

My chose is a Silverlight and a WP7 development now, but I’m more and more like iOS+Mac development.

To the best of my belief life should be funny, interesting and full of positive emotion! If nothing happens in your life – believe me, you are doing something wrong. If you e-mail box/Skype/whatever is empty, there is only one reason: you’re not needed.

Firstly I worked for one of the biggest IT company in Donetsk, Ukraine. It was OK, except that my application was completely on Ext JsJ I hate javascript and don’t want to combine my life with it. So I had no choose.

Then I was hired for Belgium ItemSolutions company. I’m happy here. I suppose that outsourcing is wrong way for company and particularly me. I’m truly belief on own products and Silverlight and everything I found here. And every more: good people and atmosphere, interesting projects, and company which is “knowledge-driven instead of business-driven.” Eventually I belief in a company and projects part of which I’m.

Of course, at the end I’d like to maintain few words about my new country:

Enjoy your life!