duminică, 15 ianuarie 2012

Converters with Parameters


Converter


[ValueConversion(typeof (object), typeof (string))]
public class StringFormatConverter : IValueConverter {
  public object Convert(object i_value, Type i_targetType, object i_parameter,                              System.Globalization.CultureInfo i_culture) {
    string _format = i_parameter as string;
    if (!string.IsNullOrEmpty(format)) {
         return string.Format(i_culture, _format, i_value);
        }
    return i_value.ToString();
  }
}


XAML

<Window.Resources>
  <HierarchicalData:Employee x:Key="myEmployee" FirstName="George" LastName="Lache" Position="DEV" Salary="2000"></HierarchicalData:Employee>
  <Converter:StringFormatConverter x:Key="StringFormatConverter" />
</Window.Resources>
<StackPanel DataContext="{StaticResource myEmployee}">
  <Label Content="{Binding FirstName, Converter={StaticResource StringFormatConverter}, ConverterParameter='Many thanks to {0}'}" />
  <Label Content="{Binding Salary, Converter={StaticResource StringFormatConverter}, ConverterParameter='My salary is thanks to {0:c}'}" />
  <Label Content="{Binding Salary, Converter={StaticResource StringFormatConverter}, ConverterParameter=\{0:c\}}" />
  <TextBox Text="{Binding Path=Salary, StringFormat=\{0:c\}}" />
</StackPanel>




Result



vineri, 13 ianuarie 2012

Readonly Collections are Wrappers over the Read/Write Collections


    class Program
    {
        static void Main(string[] args)
        {
            //Readonly Collections are Wrapper over the Read/Write Collections
            ObservableCollection<string> _observableCollection = new ObservableCollection<string>() { "Mitica", "Dragomir"};
            ReadOnlyObservableCollection<string > _readOnlyObservable = new ReadOnlyObservableCollection<string>(_observableCollection);

            _observableCollection.Add("Gigi");
            //_observableCollection and _readOnlyObservable have both 3 items.

            List<string> _list = new List<string> { "Mitica", "Dragomir" };
            List<string> _list2 = new List<string>(_list);//here is created a new list of pointers
            ReadOnlyCollection<string> _collection = new ReadOnlyCollection<string>(_list);
            
            _list.Add("Gigi");
            //_list has 3 items  // _list2 has 2 items // _collection has 3 items

            
        }
    }