Thursday, December 18, 2014

Solving WPF Binding ConverterParameter problem

Write a multiconverter instead:
<TextBlock FontSize="16" FontFamily="GE Inspira Medium" VerticalAlignment="Center" Foreground="Black">
 <TextBlock.Text >
<MultiBinding Converter="{StaticResource flowRateUnitConverter}">
<Binding Path="FlowRate"/>
<Binding Path="Unit"/>
</MultiBinding>                                   
</TextBlock.Text>
</TextBlock>


MultiConverter class
public class FlowRateUnitConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values[0] == null || values[1] == nullreturn string.Empty;
            float? flowRate=values[0] as float?;
            string unit = values[1] as string;
            string convertedValue = values[0].ToString();
            switch(unit)
            {
                case "L/Hr":
                    convertedValue = (flowRate.Value * 60).ToString();
                    break;
                default:
                    break;
 
            }
            return convertedValue;
        }
 
        public object[] ConvertBack(
            object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

Wednesday, December 17, 2014

Get/Set property value in an object instance using reflection

public static object GetPropValue(T src, string propName)
{
  return src.GetType().GetProperty(propName).GetValue(src, null);
}

public static object SetPropValue(T src, string propName, object value)
{
  return src.GetType().GetProperty(propName).SetValue(src, value);
}

Wednesday, December 10, 2014

WPF Performance Issues and knowledge about UI rendering

http://www.codeproject.com/Articles/784529/Solutions-for-WPF-Performance-Issue

Wednesday, December 3, 2014

Refer WPF dictionary in another library

<UserControl x:Class="ProcessPictureUserControls.DualSensors"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:lib="clr-namespace:PPLibrary;assembly=PPLibrary1.12.0.1" 
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d" 
             Name="UserControl"
             d:DesignHeight="40" d:DesignWidth="200">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/PPLibrary1.12.0.1;component/Themes/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

.
.
.
<Border Name="InnerBorder" CornerRadius="20" Margin="3" BorderBrush="LightGray" Background="{StaticResource ResourceKey=DarkBrush}"  BorderThickness="{Binding Path=RegulationState, Mode=OneWay, Converter={StaticResource ResourceKey=regulationStateToBorderOnOffConverter},FallbackValue=1}" />

Tuesday, December 2, 2014

BooleanToVisibilityConverter - alternative way

Quite a departure from the regular use of the visibility converter

Reference- http://architects.dzone.com/articles/goodbye?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+zones%2Fdotnet+%28.NET+Zone%29

Old Method
Visibility={Binding IsVisible,Converter={StaticResource BooleanToVisibilityConverter}}

New Method

Usage
<TextBlock Text="I am visible!"
                       Style="{StaticResource PhoneTextExtraLargeStyle}"
                       attachedProperties:Alt.IsVisible="{Binding IsVisible}"
                       />
Attached Property - Class

public static partial class Alt
    {
        public static readonly DependencyProperty IsVisibleProperty = DependencyProperty.RegisterAttached(
            "IsVisible"typeof(bool?), typeof(Alt), new PropertyMetadata(default(bool?), IsVisibleChangedCallback));

        private static void IsVisibleChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var fe = d as FrameworkElement;
            if (fe == null)
                return;

            fe.Visibility = ((bool?)e.NewValue) == true
                ? Visibility.Visible
                : Visibility.Collapsed;
        }

        public static void SetIsVisible(DependencyObject element, bool? value)
        {
            element.SetValue(IsVisibleProperty, value);
        }

        public static bool? GetIsVisible(DependencyObject element)
        {
            return (bool?)element.GetValue(IsVisibleProperty);
        }
    }