Context Menu of WPF ListView


When an user interface contain too many buttons, a user cannot comfort to handle this and i think it is not a good design.For this reason i try to remove some button which button actually need for editing,deleting operation for Listview.In this situation The Context Menu is the perfect solution.
here is the XAML Code

<ListView BorderBrush="#FFA8CC7B" Height="133" HorizontalAlignment="Left" Margin="88,382,0,0" Name="lvInvDetails" VerticalAlignment="Top" Width="596">
            <ListView.ContextMenu>
                <ContextMenu Name="InvoiceDetailsList" StaysOpen="true" Background="WhiteSmoke">
                    <ContextMenu.BitmapEffect>
                        <BitmapEffectGroup/>
                    </ContextMenu.BitmapEffect>
                    <MenuItem Header="Edit" Name="EditIVD" Click="EditIVD_Click" />
                    <MenuItem Header="Remove" Name="RemoveIVD" Click="RemoveIVD_Click" />
                </ContextMenu>
            </ListView.ContextMenu>
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=InvoiceDetailsItemId}" Header="Item Id" Width="90" />
                    <GridViewColumn DisplayMemberBinding="{Binding Path=InvoiceDetailsItemName}" Header="Item Name" Width="120" />
                </GridView>
            </ListView.View>
        </ListView>

If you have any query about this topic please leave a comment.

WPF Listview Alternate Row Color


For setting the background color of Listview rows in an alternate fashion (odd rows and even rows) at first create a style element :

 <Style x:Key="alternatingStyle" TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                <Setter Property="Background" Value="LightSkyBlue"></Setter>
            </Trigger>
            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                <Setter Property="Background" Value="LightGray"></Setter>
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Orange"/>                
            </Trigger>
        </Style.Triggers>
    </Style>

Now write this XAML code for ListView

<ListView BorderBrush="#FFA8CC7B" Height="133" HorizontalAlignment="Left" Margin="88,382,0,0" Name="lvInvDetails" VerticalAlignment="Top" Width="596"
                  ItemContainerStyle="{StaticResource alternatingStyle}" AlternationCount="2">
    <ListView.View>
        <GridView>
        <GridViewColumn DisplayMemberBinding="{Binding Path=InvoiceDetailsItemId}" Header="Item Id" Width="90" />
        <GridViewColumn DisplayMemberBinding="{Binding Path=InvoiceDetailsItemName}" Header="Item Name" Width="120" />                    
        </GridView>
    </ListView.View>
</ListView>

Now You can see alternation color of listview.
If you have any query about this topic please leave a comment.

Autosuggest Textbox for WPF


In this post i will share to everyone how to make an auto suggest TextBox in wpf like GOOGLE Textbox.
For this i create a grid whose first row contains a textbox and the second row contains a Listbox. Now, when the textbox gets the focus and its text changes due to the user’s input then fill up the listbox.
Here is the XAML code

<Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="111,46,0,0" Name="txtAutoSuggestName" VerticalAlignment="Top" Width="161" PreviewKeyDown="txtAutoSuggestName_PreviewKeyDown" TextChanged="txtAutoSuggestName_TextChanged" />
        <ListBox Height="Auto" HorizontalAlignment="Left" Margin="111,69,0,0" Name="listBoxSuggestion" VerticalAlignment="Top" Width="161" Visibility="Hidden" PreviewKeyDown="listBoxSuggestion_PreviewKeyDown" KeyDown="listBoxSuggestion_KeyDown" />
    </Grid>

Now this is CS code for glorious effect in textbox

 private void txtAutoSuggestName_TextChanged(object sender, TextChangedEventArgs e)
        {
            listBoxSuggestion.Items.Clear();
            if (txtAutoSuggestName.Text != "")
            {
                List<Customer> namelist = CustomerGatewayObj.listShow(txtAutoSuggestName.Text);
                if (namelist.Count > 0)
                {
                    listBoxSuggestion.Visibility = Visibility.Visible;
                    foreach (var obj in namelist)
                    {
                        listBoxSuggestion.Items.Add(obj.id);
                    }
                }
            }
            else
            {
                listBoxSuggestion.Visibility = Visibility.Hidden;
            }
        }

        private void txtAutoSuggestName_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Down)
            {
                listBoxSuggestion.Focus();
            }
        }


        private void listBoxSuggestion_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (listBoxSuggestion.SelectedIndex == 0 && e.Key == Key.Up)
            {
                txtAutoSuggestName.Focus();
            }
        }

        private void listBoxSuggestion_KeyDown(object sender, KeyEventArgs e)
        {
            if (listBoxSuggestion.SelectedIndex > -1)
            {
                if (e.Key == Key.Enter)
                {
                    txtAutoSuggestName.Text = listBoxSuggestion.SelectedItem.ToString();
                }
            }
        }

Create PopUp Window in C# WPF


To create PopUp window WPF provide a popup tag.when i click a button then the pop-up window is open.This pop-up window contain a listview.The pop-up window is closed when leaving the mouse from other place.
Here is the code for creation pop-up window

<Popup Name="PopupEsales" Placement="Right" IsEnabled="True" IsOpen="False" Grid.RowSpan="2">
    <ListView Height="145" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="lvSalesPersonIdSearch" VerticalAlignment="Top" Width="257" SelectionChanged="lvSalesPersonIdSearch_SelectionChanged">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Sales Persons Id" Width="100" DisplayMemberBinding="{Binding Path=SPID}" />
                <GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Path=Name}" />
            </GridView>
        </ListView.View>
    </ListView>
</Popup>

This following code under the button to open the pop-up window

private void btnSearchInfo_Click(object sender, RoutedEventArgs e)
    {
        PopupEsales.IsOpen = true;
        lvSalesPersonIdSearch.Items.Add(aESales);
    }

Pop-Up Close

        private void LayoutRoot_MouseLeave(object sender,MouseEventArgs e)
        {
            PopupEsales.StaysOpen = false;
        }

If you have any query about this topic please leave a comment.

How to Add Row of a WPF Listview when Press “Enter” Key in Listview Column


I saw many application software provides this kind of facilities.But WPF is new for everybody.So i think for doing this in c# WPF.In this post i will discuss about this solution.
My ListView contain one column which is binding by a object.At first i make a Listview which Column field contain textbox.Here is the XAML Code.

<ListView Height="101" Margin="289,318,0,0" Name="lvItem" VerticalAlignment="Top" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" Width="215">
    <ListView.View>	
        <GridView>
            <GridViewColumn Header="Item Name"  Width="80">
                <GridViewColumn.CellTemplate >
		    <DataTemplate>
                        <TextBox Name="txtForAddRow"  Text="{Binding Path=ItemName}" 
                                 Margin="-6,0,-6,0" KeyDown="txtForAddRow_KeyDown" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>  

The entity class definition is here

public class EItemCategory
{
public string ItemName { get; set; }
}

Now the cs Code for add row in Listview

private void txtForAddRow_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key==Key.Enter)
{
EItemCategory obj = new EItemCategory();
lvItem.Items.Add(obj);
}
}

If you have any query about this topic please leave a comment.