我有两个窗口,一个有访问者列表,另一个用于显示它。如果我选择一个访问者(示例SUNDAR)并在另一个页面上显示,如何显示
Xaml访客
<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" />
代码
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"/>
码
private void VisitorNo_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void Name_TextChanged(object sender, TextChangedEventArgs e)
{
}
我已经从访问者的数据库建立连接
答案 0 :(得分:0)
首先,您需要定义一个class
Visitor
,其中包含Visitor
(Name,VisitorNo ..)的结构,然后定义包含该列表的ObservableCollection
您的访问者并将您的列表绑定到该集合,并且不要忘记设置DataContext
。
每次按下按钮,您都可以将Dalog的DataContext
设置为所选的访问者,此处为完整代码:
首先:对于主窗口 XAML
<StackPanel>
<ListView x:Name="listVisitor" ItemsSource="{Binding ListVisitors}" DisplayMemberPath="Name" />
<Button Content="View Visitor" Click="ViewBtn_Click" />
</StackPanel>
和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();
}
}
}
第二次这里的对话是他的xaml
<StackPanel>
<TextBox Text="{Binding VisitorDataContext.Name,Mode=TwoWay}"/>
<TextBox Text="{Binding VisitorDataContext.VisitorNo,Mode=TwoWay}"/>
<Button Content="Close" Click="ButtonBase_OnClick"></Button>
</StackPanel>
及其背后的代码
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();
}
}
使用其构造函数将选定的Visitor传递给对话框。