Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, June 19, 2014

C# - Enum Fully Qualified Name with space and Display

In Enum we can not have fully qualified name with spaces. To achieve that we can add the description attribute as follows

     ....
    using System.ComponentModel;
   ....

    public enum Employee
    {
        [Description("Balajiprasad Ramesh")]
        BalajiprasadRamesh = 1,
      
        [Description("Combined Name")]
        CombinedName = 2

    }



To display Enum description we can use the following code

private static string GetEnumDescription(object value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                typeof(DescriptionAttribute),
                false);

            if (attributes != null &&
                attributes.Length > 0)
                return attributes[0].Description;
            else
                return value.ToString();
        }

Thursday, November 28, 2013

WPF - How to Show multiple overlays in single view

Problem Statement:

Showing multiple overlays in single view using WPF Prism region manager and regions


Solution:

Have region using ItemsControl or Listbox

Listbox XAML Sample

     <ListBox x:Name=" MultipleOverlayRegion "
                         PrismRegions:RegionManager.RegionName="MultipleOverlayRegion">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid></Grid>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <ContentControl Content="{Binding}" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem"
               BasedOn="{StaticResource {x:Type ListBoxItem}}">
                    <Setter Property="VerticalContentAlignment"
                    Value="Stretch" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <ContentPresenter />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>

        </ListBox>


ItemsControl XAML Sample

  <ItemsControl x:Name="MultipleOverLayRegion">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid></Grid>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
  </ItemsControl>

Bind your regions with multiple views to show as overlay

  regionManager.Regions[MultipleOverlayRegion].Add(overlayView1);
  regionManager.Regions[MultipleOverlayRegion].Add(overlayView2);

WPF - Mutually Exclusive Listboxes and maintain single selection

Problem Statement:

I have two listboxes and i need to maintain single selection across the page within the listboxes.

Solution:

Keep SelectedItem as single and have like below

private Employee selectedItem;
public Employee SelectedItem{
    get
    {
        return selectedItem;
    }
    set
    {
       //For clearing existing selection
        selectedItem = null;
        NotifyPropertyChanged("SelectedItem");

        //For assigning new selection
        selectedItem = value;
        NotifyPropertyChanged("SelectedItem");
    }

Wednesday, August 21, 2013

WPF - Access any control inside Template control

Following is code to get the control
[ControlType] objName = ([ControlType])ParentControlName.Template.FindName("ControlName", ParentControlName);


Ex:- 
Below is the code to get the scroll viewer of combo box and set to top

ScrollViewer cmbScrollViewer = ScrollViewer)cmbName.Template.FindName("DropDownScrollViewer", cmbName);

cmbScrollViewer.ScrollToHome();
  

Style Control template of combo box maybe as below

<ControlTemplate TargetType="{x:Type ComboBox}">
…. 
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"x:Name="DropDownScrollViewer" Template="{DynamicResource ScrollViewerTemplate}" MaxWidth="330"MaxHeight="{TemplateBinding MaxDropDownHeight}">
        <ItemsPresenter/>
</ScrollViewer>
…. 

</ControlTemplate>

Wednesday, August 7, 2013

C# - How to get Property Name as string by Property Type

Create a static class for Property Helper with GetPropertyName method

public static class PropertyHelper<T>
    {
        public static string GetPropertyName<TPropObject>(Expression<Func<T, TPropObject>> propertyValue)
        {
            return ((MemberExpression)propertyValue.Body).Member.Name;
        }
    }


Call like below to get the PropertyName as string


PropertyHelper<YourClass>.GetPropertyName(r => r.YourProperty)

Friday, July 12, 2013

WPF - Different ways to get System IP Address

Following method get the local from the list of Addresses as it has more IP address if it has more adapters

private object GetLocalIPAddressFromAddressList()
        {
            var hostName = Dns.GetHostName();
            var ipEntry = Dns.GetHostEntry(hostName);

            var result = from address in ipEntry.AddressList
                    where address.AddressFamily == AddressFamily.InterNetwork
                    select address;
            return result.LastOrDefault();

        }


Using Ping method to get the IP Address, it helpful to find others IP address also

        public IPAddress GetIPAddressUsingPing()
        {
            string hostName = "rbalajiprasad.blogspot.com";
            //You can get local Computer IP address also by sending HostName by following
            //string hostName = System.Net.Dns.GetHostName();


            Ping ping = new Ping();
            var replay = ping.Send(hostName);

            if (replay.Status == IPStatus.Success)
            {
                return replay.Address;
            }
            return null;
        }

Monday, May 13, 2013

C# - Delegate Find method using Predicate


Delegate Find method using Predicate
static Predicate<Car> ByYear(int year)
{
    return delegate(Car car)
    {
        return car.Year == year;
    };
}

static void Main(string[] args)
{
    
    List<Car> list = new List<Car>
    {
        new Car { Year = 1940 },
        new Car { Year = 1965 },
        new Car { Year = 1973 },
        new Car { Year = 1999 }
    };
    var car99 = list.Find(ByYear(1999));
    var car65 = list.Find(ByYear(1965));

    Console.WriteLine(car99.Year);
    Console.WriteLine(car65.Year);
}