0

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 enter image description here

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
Un-Known
  • 43
  • 5
  • What do you mean by "Display back" ?! If you want to pass Visitor data to your second (detailed information) window you can easily pass a reference to this window and pick it up for databinding to user controls...Or pass an ID of a visitor, retrieve info from database and bind it to user controls. – Youp Bernoulli Feb 17 '15 at 07:32
  • Let just say i pick Visitor name Sundar,then I click button View Visitor,after that it open new Window, carry Visitor Name Sundar from Visitor page and Display on Textbox that I provide in View list – Un-Known Feb 17 '15 at 07:37
  • Is http://stackoverflow.com/questions/15091400/get-single-listview-selecteditem this thread relevent to this question??? – Amol Bavannavar Feb 17 '15 at 08:20
  • I using two Window page,Visitor and Viewlist,I want to pass selected data from Vistor to second Window,Viewlist – Un-Known Feb 17 '15 at 08:37

1 Answers1

0

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.

SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47