简体   繁体   中英

Display selected data in list view

I have two window,one have list of visitor,another to display it.How to display back if I select one visitor(example SUNDAR) and display back on another page

Xaml Visitor

<ListView x:Name="listVisitor"  ItemsSource="{Binding}" HorizontalAlignment="Left" Height="582" Margin="110,65,0,0" VerticalAlignment="Top" Width="924" Grid.ColumnSpan="3" />

<Button Content="View Visitor"  Foreground="white" HorizontalAlignment="Left" Margin="98,674,0,0" VerticalAlignment="Top" Width="320" Height="30" Background="#FF1CA0B7" Grid.ColumnSpan="2" Name="ViewBtn" Click="ViewBtn_Click" />

Code

private void ViewBtn_Click(object sender, RoutedEventArgs e)
{
    ViewList dialogBox = new ViewList();`

    // Show window modally 
    // NOTE: Returns only when window is closed
    Nullable<bool> dialogResult = dialogBox.ShowDialog();

}

Viewlist

 <TextBox  Name="VisitorNo"/>
 <TextBox Name="Name"/>

code

private void VisitorNo_TextChanged(object sender, TextChangedEventArgs e)       
{

}

private void Name_TextChanged(object sender, TextChangedEventArgs e)
{

}

I already make connection from database for Visitor 在此输入图像描述

First you need to define a class Visitor that hold the structure of a Visitor (Name, VisitorNo ..), then define an ObservableCollection that contains the list of your visitors and bind your list to that collection, and don't forget to set the DataContext . Each time you press the button you set the DataContext of the Dalog to the selected Visitor, Here the full code :

First : For the main Window Xaml

 <StackPanel>
    <ListView x:Name="listVisitor"  ItemsSource="{Binding ListVisitors}" DisplayMemberPath="Name"  />
    <Button Content="View Visitor"    Click="ViewBtn_Click" />
</StackPanel>

and the codeBehind

public class Visitor
{
    public String Name { get; set; }        
    public int VisitorNo { get; set; }
}

public partial class MainWindow : Window
{
    public ObservableCollection<Visitor> ListVisitors { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        ListVisitors=new ObservableCollection<Visitor>()
        {
            new Visitor()
            {
                Name = "name1",                    
                VisitorNo=21
            },
             new Visitor()
            {
                Name = "name2",                    
                VisitorNo=21
            },
             new Visitor()
            {
                Name = "name3",                    
                VisitorNo=21
            }
        };

    }

    private void ViewBtn_Click(object sender, RoutedEventArgs e)
    {
        if (listVisitor.SelectedItem!=null)
        {
            var dialogBox = new Viewlist((Visitor)listVisitor.SelectedItem);
            var dialogResult = dialogBox.ShowDialog();
        }


    }
}

Second for the Dialog here his xaml

    <StackPanel>

    <TextBox  Text="{Binding VisitorDataContext.Name,Mode=TwoWay}"/>
    <TextBox  Text="{Binding VisitorDataContext.VisitorNo,Mode=TwoWay}"/>
    <Button Content="Close" Click="ButtonBase_OnClick"></Button>
</StackPanel>

and its code behind

public partial class Viewlist : Window
{
    public Visitor VisitorDataContext { get; set; }    
    public Viewlist(Visitor visitor)
    {
        InitializeComponent();
        this.DataContext = this;
        VisitorDataContext = visitor;
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
}

the selected Visitor is passed to the dialog using its constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM