我是一个新手,正在尝试一个"简单"的程序,甚至不知道如何正确提问,但让我再试一次。 感谢用户 Ben 帮助我走到了这一步!
我正在尝试一个简单的签入和签出程序,该程序从列表中填充姓名,并带有一个允许用户更改其状态并发表评论的按钮(例如:下午 2 点返回 - 市中心会议(。
这是我的主窗口.CS:
namespace SimpleInOut
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Test> items = new List<Test>();
lvUsers.Items.Add(new Test() { Name = "User A" });
lvUsers.Items.Add(new Test() { Name = "User B" });
lvUsers.Items.Add(new Test() { Name = "User C" });
}
public MainWindow(string strUserComment)
{
InitializeComponent();
List<Test> items = new List<Test>();
lvUsers.Items.Add(new Test() { Name = "User A" });
lvUsers.Items.Add(new Test() { Name = "User B" });
lvUsers.Items.Add(new Test() { Name = "User C" });
MessageBox.Show(String.Format("Your comment is: {0}", strUserComment));
MessageBox.Show(String.Format("Your userNumber is: {0}", userNumber));
//Message box checks above are correct
//However, it is not changing the comment box to the strUserComment - why?!
Test t = (Test)lvUsers.Items[userNumber];
t.Comment = strUserComment;
lvUsers.Items.Refresh();
}
public static string strUserComment { get; set; }
public static int userNumber { get; set; }
private void Button_Click(object sender, RoutedEventArgs e)
{
//Get row number
var item = (sender as FrameworkElement).DataContext;
userNumber = lvUsers.Items.IndexOf(item);
SubWindow subWindow = new SubWindow();
subWindow.Show();
}
}
public class Test
{
public string Name { get; set; }
public string Comment { get; set; }
}
}
这是我的子窗口.cs:
namespace SimpleInOut
{
/// <summary>
/// Interaction logic for SubWindow.xaml
/// </summary>
public partial class SubWindow : Window
{
public SubWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string strUserComment = userCommentBox.Text;
//MessageBox.Show(String.Format("Your comment was: {0}", strUserComment));
this.Close();
MainWindow subWindow = new MainWindow(strUserComment);
}
}
}
关键问题在主窗口中.CS我检查用户编号和注释是否正确地从子窗口传递 - 它们是。 但是,我似乎无法在列表中显示注释(strUserComment(。
如果需要,下面是 MainWindow XAML:
<ListView x:Name="lvUsers">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Path=Name}" />
<GridViewColumn Header="Comment" Width="150" DisplayMemberBinding="{Binding Path=Comment}" />
<GridViewColumn Header="Button">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Margin="6,2,6,2">
<Button Content="Click" Click="Button_Click" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
这是子窗口 XAML:
<Grid Margin="8">
<TextBox Name="userCommentBox" DockPanel.Dock="Top" Height="25" MaxLength="50" />
<Button HorizontalAlignment="Right" VerticalAlignment="Center" Height="25" Width="40" Click="Button_Click">OK</Button>
</Grid>
这是一个简单的例子,它是如何工作的。我正在使用子窗口的Closed
事件将信息返回到主窗口。
主窗口.cs:
public MainWindow()
{
InitializeComponent();
List<Test> items = new List<Test>();
lvUsers.Items.Add(new Test() { Name = "User A" });
lvUsers.Items.Add(new Test() { Name = "User B" });
lvUsers.Items.Add(new Test() { Name = "User C" });
}
private int clickedRowIndex = -1;
private void Button_Click(object sender, RoutedEventArgs e)
{
var item = (sender as FrameworkElement).DataContext;
clickedRowIndex = lvUsers.Items.IndexOf(item);
SubWindow subWindow = new SubWindow();
subWindow.Closed += SubWindow_Closed;
subWindow.Show();
}
private void SubWindow_Closed(object sender, EventArgs e)
{
SubWindow sw = (SubWindow)sender;
Test t = (Test)lvUsers.Items[clickedRowIndex];
t.Comment = sw.UserComment;
lvUsers.Items.Refresh();
}
子窗口.cs:
public SubWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
UserComment = userCommentBox.Text;
Close();
}
public string UserComment { get; set; }