Posts Tagged ‘c#’

Define some items in a ListBox as unselectable

No Comments »

It seems to be a quite common problem that one does not know how to set one or more items in a ListBox to unselectable or disabled.

The same principle as described here can also be user for similar problems like setting a different background color for some items.

First we’re going to create a simple domain class with a property that describes if the item should be selectable or not.

public class MyDataObject {
    public string Name { get; set; }
    public bool IsSelectable { get; set; }
}

Next we create a XAML Window that contains a ListBox. In this ListBox we define a custom ItemContainerStyle with a Style that defines if the Item will be selected or not. And finally we bind this Style to the IsSelectable property from our data object.



    
        
            
                
                    
                    
                    
                
            
        

        
            
        
    

And now we just have to fill the ItemsSource from the ListBox and we can run the example.

public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();

        myListBox.ItemsSource =
            from i in Enumerable.Range(0, 10)
            select new MyDataObject() { Name = string.Format("Object {0}", i), IsSelectable = i % 3 != 0 };
    }
}

That’s all. Easy, isn’t it?

An easy way to revert changes in Object data

No Comments »

A quite common problem in an MVC GUI is the need to revert changes in the model made by the user. Let’s say you create a UI where the user can modify some fields that are bound to a model object. After the user already did some changes, he recognizes that he entered some wrong data and wants to cancel the editing. So what now? The data is already written into the model.

The easiest way to revert this changes is obviously to let the user work with a copy of the real data object. So let’s add a clone method to the model and mark it as Serializable.

Model with Clone() method

[Serializable]
public abstract class PersonModel {

    public string Name { get; set; }
    public string FirstName { get; set; }
    public int Age { get; set; }

    public PersonModel Clone() {
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new MemoryStream();
        using (stream) {
            formatter.Serialize(stream, this);
            stream.Seek(0, SeekOrigin.Begin);
            return (PersonModel)formatter.Deserialize(stream);
        }
    }
}

We now just bind the copy of the object to the view:

myView.DataContext = person.Clone();

This copy can easily be dropped in case the user cancels the editing. In case he saves the
changes, you can just replace the original object with the copy.

One problem I ran in it was, that I normally implement the INotifyPropertyChanged interfaces to my models to inform the view about changes in the model. Unfortunately the PropertyChangedEventHandler contained in this interface causes a nasty SerializationException because the Class PropertyChangedEventManager is not marked as Serializable.

System.Runtime.Serialization.SerializationException was caught
Message=”Type ‘System.ComponentModel.PropertyChangedEventManager’ in Assembly ‘WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35′ is not marked as serializable.”

The easiest way to work around this problem is to just exclude the PropertyChangedEventHandler from the serialization.

Clonable Model that implements INotifyPropertyChanged

[Serializable]
public abstract class PersonModel : INotifyPropertyChanged {
    [field: NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;

    private string name;
    private string firstName;
    private int age;

    public string Name {
        get { return name; }
        set { name = value; OnPropertyChange(); }
    }
    public string FirstName {
        get { return firstName; }
        set { firstName = value; OnPropertyChange(); }
    }
    public int Age {
        get { return age; }
        set { age = value; OnPropertyChange(); }
    }

    public PersonModel Clone() {
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new MemoryStream();
        using (stream) {
            formatter.Serialize(stream, this);
            stream.Seek(0, SeekOrigin.Begin);
            return (PersonModel)formatter.Deserialize(stream);
        }
    }

    protected void OnPropertyChange() {
        string propName = new StackFrame(1).GetMethod().Name.Replace("set_", "");
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

In this way everything works fine and you have hopefully one problem less to care about…

Happy DataBinding :-)

Access a Zend Rest Service from C#

1 Comment »

Accessing a Restful service is actually no big deal, but it took me quite a while to figure out some details. So here’s a short description of it.
Read the rest of this entry »