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;
        }

Thursday, July 11, 2013

WPF - Export to excel using EPPlus

Following code helpful to create a excel file from WPF

Prerequesties:-
EPPlus DLL   (Ref: http://epplus.codeplex.com/releases/view/89923)

Step 1: Create a WPF project

Step 2:  Add reference of EPPlus DLL

Step 3: Following is the sample class to export

        public class Employee
        {
            public string EmployeeName;
            public int EmployeeAge;
            public string Designation;
        }


Step 4: Create Data with values

            List<Employee> lstEmployee = new List<Employee>();
            lstEmployee.Add(new Employee() { EmployeeName = "Balaji", EmployeeAge = 16, Designation = "DE" });
            lstEmployee.Add(new Employee() { EmployeeName = "prasad", EmployeeAge = 16, Designation = "TA" });

           
Step 5: Create Header Data for excel
           List<string> lstHeader = new List<string>() { "Employee Name", "Employee Age", "Designation" };
Step 6: Create Excel Package and add Header and Column details

            ExcelPackage pck = new ExcelPackage();
            pck.Workbook.Properties.Author = "Balajiprasad";
            pck.Workbook.Properties.Title = "EPPlus in WPF";
            pck.Workbook.Properties.Company = "For Aditi Technologies";

            var ws = pck.Workbook.Worksheets.Add("Employee Details");

            //Header Section
            for (int i = 0; i < lstHeader.Count; i++)
            {
                ws.Cells[1, i + 1].Value = lstHeader[i];
                ws.Cells[1, i + 1].Style.Font.Bold = true;
            }

            //Column Value Section
            for (int i = 0; i < lstEmployee.Count; i++)
            {
                ws.Cells[i + 2, 1].Value = lstEmployee[i].EmployeeName;
                ws.Cells[i + 2, 2].Value = lstEmployee[i].EmployeeAge;
                ws.Cells[i + 2, 3].Value = lstEmployee[i].Designation;
            }


Step 7: Save as the byte array as excel

            byte[] fileText = pck.GetAsByteArray();

            SaveFileDialog dialog = new SaveFileDialog()
            {
                Filter = "Excel Worksheets (*.xlsx)|*.xlsx"
            };

            if (dialog.ShowDialog() == true)
            {
                File.WriteAllBytes(dialog.FileName, fileText);
            }


Following is the entire code

        private void GenerateExcel()
        {
            List<Employee> lstEmployee = new List<Employee>();
            lstEmployee.Add(new Employee() { EmployeeName = "Balaji", EmployeeAge = 16, Designation = "DE" });
            lstEmployee.Add(new Employee() { EmployeeName = "prasad", EmployeeAge = 16, Designation = "TA" });

            List<string> lstHeader = new List<string>() { "Employee Name", "Employee Age", "Designation" };


            ExcelPackage pck = new ExcelPackage();
            pck.Workbook.Properties.Author = "Balajiprasad";
            pck.Workbook.Properties.Title = "EPPlus in WPF";
            pck.Workbook.Properties.Company = "For Aditi Technologies";

            var ws = pck.Workbook.Worksheets.Add("Employee Details");

            //Header Section
            for (int i = 0; i < lstHeader.Count; i++)
            {
                ws.Cells[1, i + 1].Value = lstHeader[i];
                ws.Cells[1, i + 1].Style.Font.Bold = true;
            }

            //Column Value Section
            for (int i = 0; i < lstEmployee.Count; i++)
            {
                ws.Cells[i + 2, 1].Value = lstEmployee[i].EmployeeName;
                ws.Cells[i + 2, 2].Value = lstEmployee[i].EmployeeAge;
                ws.Cells[i + 2, 3].Value = lstEmployee[i].Designation;
            }

            byte[] fileText = pck.GetAsByteArray();

            SaveFileDialog dialog = new SaveFileDialog()
            {
                Filter = "Excel Worksheets (*.xlsx)|*.xlsx"
            };

            if (dialog.ShowDialog() == true)
            {
                File.WriteAllBytes(dialog.FileName, fileText);
            }

        }


public class Employee
        {
            public string EmployeeName;
            public int EmployeeAge;
            public string Designation;
        }