繁体   English   中英

列表视图中的WPF更新列

[英]WPF Update column in listview

我是一个新手,正在尝试尝试“简单”程序,甚至不知道如何正确地提出这个问题,但是让我再尝试一次。 感谢用户Ben帮助我做到了这一点!

我正在尝试一个简单的签入和签出程序,该程序从列表中填充名称,并带有一个按钮,允许用户更改其状态并发表评论(例如:回到下午2点-市区会议)。

这是我的MainWindow.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; }
    }
}

这是我的Subwindow.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);
    }
}
}

关键问题在Mainwindow.CS中,在这里我检查用户号和注释是否正确地从Subwindow中传递了。 但是,我似乎无法使注释(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>

这是一个如何工作的简单示例。 我正在使用SubWindow的Closed事件将信息返回到MainWindow。

MainWindow.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();
}

SubWindow.cs:

public SubWindow()
{
    InitializeComponent();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    UserComment = userCommentBox.Text;            
    Close();
}

public string UserComment { get; set; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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