none
Adding Items to ListView Columns in c# WPF. RRS feed

  • Question

  • Hiii,

    I have one TextBox is for entering UserName and PasswordBox for Password. and 1 ADD Button to add items to  ListView Control.

    My Question is : Is this posible to ADD Both Username and Password to listview ?? If Possible how can i do program for that.??

    Anyone knows Please Answer me..

    <TextBox x:Name="Textbox_SOTAUName" 
     Margin="0,26,563,0" HorizontalAlignment="Stretch"
     Grid.Column="1" /> 
    
    <PasswordBox HorizontalAlignment="Stretch" Grid.Column="1"  x:Name="Passwordbox_SPassword" ClipToBounds="False" MaxLength="8" PasswordChar="*" 
    VerticalAlignment="Stretch" Foreground="#FF083042" Margin="-2">  
    
    <PasswordBox.BorderBrush> 
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">            
             <GradientStop Color="Black" Offset="0"/>               
             <GradientStop Color="#FFFFE9EF" Offset="1"/> 
        </LinearGradientBrush>                
    </PasswordBox.BorderBrush>                  
    </PasswordBox>
    
    <ListBox x:Name="ListBox_User_Pass" Margin="19,30,265,18" Visibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Auto" 
    Grid.ColumnSpan="2" Grid.Column="2" Grid.Row="4" RenderTransformOrigin="0.503,0.306" FontSize="13" FontWeight="Bold">               
    <ListBox.RenderTransform>                
         <TransformGroup>                
            <ScaleTransform/>                
                <SkewTransform AngleX="-0.972"/>                 
                <RotateTransform/>                  
                <TranslateTransform X="-0.613"/>                   
       </TransformGroup>                    
    </ListBox.RenderTransform>                  
    
    </ListBox> 



    Thank you,

    • Moved by CoolDadTx Saturday, December 14, 2019 7:37 PM WPF related
    Saturday, December 14, 2019 7:08 AM

All replies

  • Hello,

    Here is a very basic example.

    <Window x:Class="Basics.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Basics"
            mc:Ignorable="d"
            Title="Window1" Height="450" Width="800">
    
    
        <Grid>
    
            <Grid.RowDefinitions>
                <RowDefinition Name="Row1" Height="175"/>
                <RowDefinition Name="Row2" Height="71"/>
                <RowDefinition Name="Row3" Height="50"/>
            </Grid.RowDefinitions>
            <StackPanel>
                <ListView  
                    VerticalAlignment="Stretch" 
                    HorizontalAlignment="Stretch" 
                    Height="Auto" Width="Auto" 
                    Name="ListViewTable" 
                    Background="Transparent" >
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding UserName}" />
                            <GridViewColumn Header="Pass" Width="50" DisplayMemberBinding="{Binding UserPassword}" />
                        </GridView>
                    </ListView.View>
                </ListView>
            </StackPanel>
            <TextBox 
                x:Name="UserName" 
                HorizontalAlignment="Left" 
                Height="23" 
                Margin="10,20,0,0" 
                Grid.Row="1" 
                TextWrapping="Wrap" 
                Text="" 
                VerticalAlignment="Top" 
                Width="120"/>
            <PasswordBox 
                x:Name="UserPassword" 
                HorizontalAlignment="Left" 
                Height="23" 
                Margin="142,18,0,0" 
                Grid.Row="1" 
                VerticalAlignment="Top" 
                Width="120" 
                PasswordChar="*"  />
    
            
            <Button 
                x:Name="AddButton" 
                Content="Add" 
                HorizontalAlignment="Left" 
                Margin="284,18,0,0" 
                Grid.Row="1" 
                VerticalAlignment="Top" 
                Width="75" 
                Click="AddButton_Click" 
                Height="22"/>
        </Grid>
    </Window>
    

    Code behind

    public partial class Window1 : Window
    {
        public ObservableCollection<User> UsersList = 
            new ObservableCollection<User>();
    
        public Window1()
        {
            InitializeComponent();
            ListViewTable.ItemsSource = UsersList;
        }
    
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            UsersList.Add(new User(UserName.Text, UserPassword.Password));
        }
    }
    public class User
    {
        public string UserName { set; get; }
        public string UserPassword { set; get; }
        public User(string userName, string userPassword)
        {
            UserName = userName;
            UserPassword = userPassword;
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, December 14, 2019 12:41 PM
  • It's not normal to be using a Listview for username and password data entering at the keyboard, which should be done with two Textboxes. 

    A ListView is for working with a collection of data such a List or a datatable containing data is loaded into a ListView.

    Anyway, WPF can be discussed at the WPF forum.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wpf

    Saturday, December 14, 2019 12:47 PM