仅当单击表的标题时才显示DataGrid表值。 [英] DataGrid table values are displayed only when clicking the header of the table..

查看:108
本文介绍了仅当单击表的标题时才显示DataGrid表值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在DataGrid表中添加了一些值,我可以添加,但只有当我点击DataGrid表的标题时才会显示这些值。

我正在关注MVVM,只有当我在单独的ViewModal文件中编写代码而不是代码后才会出现此问题。



有人请为这个问题提供解决方案...



我在下面分享了我的代码。







Hi,
I'm adding some values in a DataGrid table, I can add but the values are displaying only when i click the header of DataGrid table.
I'm following MVVM, this problem arise only when i'm writing code in seperate ViewModal file instead code behind.

someone please give solution for this problem...

I have shared my code below.



 <Label Content="Add Load Generator" Grid.Row="0"  Grid.Column="1"  FontFamily="Ebrima" FontSize="15" HorizontalAlignment="left" VerticalAlignment="bottom" ></Label>
                <TextBox Text="{Binding Path=TxtIpAddress}"  Grid.Row="0" Grid.Column="2" Width="220"  Height="25" HorizontalAlignment="left" VerticalAlignment="bottom"  />
<pre lang="xml"><Button Content="Add" Command="{Binding AddIp}"  Grid.Row="0" Grid.Column="1" FontFamily="Ebrima" FontSize="12" Width="60" Height="25"  HorizontalAlignment="right" VerticalAlignment="center"/>




<DataGrid ItemsSource="{Binding SystemInformation}" AutoGenerateColumns="false"     Grid.Row="0" Grid.Column="1" CanUserAddRows="False">
   <DataGrid.Columns>
   <DataGridTextColumn Binding="{Binding Sno}"  Header="S.No" MinWidth="50" />
   <DataGridTextColumn Binding="{Binding strIpAddr}" Header="System Name" MinWidth="240"/>
   <DataGridTextColumn Binding="{Binding strSystemName}" Header="IP Address" MinWidth="240" />
   <DataGridTextColumn Binding="{Binding strStatus}" Header="Status" MinWidth="140" />
   </DataGrid.Columns>
</DataGrid>
</pre>










using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Prism.Mvvm;

using LoadTest_LibraryFiles.Views;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using System.Collections.ObjectModel;
using Microsoft.Win32;

//using Microsoft.Win32.OpenFileDialog;
using System.IO;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using System.Net;

namespace LoadTest_LibraryFiles.ViewModal
{

    public class UserBase_ViewModal : BindableBase
    {

 private int num = 1;
        ObservableCollection<HostSystemInformation> _HostSystemInformation = new ObservableCollection<HostSystemInformation>();
         public UserBase_ViewModal()
        {
           
            SystemInformation = new List<HostSystemInformation>();
            addIp = new DelegateCommand(AddSystemInformationInIpTable);
        }

 private String txtIpAddress;
        public String TxtIpAddress
        {
            get { return txtIpAddress; }
            set { SetProperty(ref txtIpAddress, value); }
        }


        private DelegateCommand addIp;
        public ICommand AddIp
        {
            get { return addIp; }
        }

        private List<HostSystemInformation> systemInformation;
        public List<HostSystemInformation> SystemInformation
        {
            get { return systemInformation; }
            set { SetProperty(ref systemInformation, value); }
        }



   private void AddSystemInformationInIpTable()
        {
            try
            {
              
                #region Add IP Address in IP Table

                systemInformation.Add(new HostSystemInformation
                {
                    Sno = num++,
                    strIpAddr = txtIpAddress,
                    strSystemName = hostname,
                    strStatus = "connected"

                });
                OnPropertyChanged(() => SystemInformation);
                
                #endregion


            }
            catch (System.ArgumentException) { MessageBox.Show("Enter the IP Address"); }
            catch (System.Net.Sockets.SocketException) { MessageBox.Show("Host not connected / No such host"); }
        }
}

 public class HostSystemInformation
    {
        public int Sno { get; set; }
        public string strIpAddr { get; set; }
        public string strSystemName { get; set; }
        public string strStatus { get; set; }
    }
         
}









提前致谢....

R.Karthik





Thanks in advance....
R.Karthik

推荐答案

您应该使用ObservableCollection而不是List。将SystemInformation声明为:

You should use ObservableCollection instead of List. Declare SystemInformation as:
public ObservableCollection<HostSystemInformation> SystemInformation { get; private set; }





然后在构造函数中初始化它:



Then initialize it in constructor:

SystemInformation = new ObservableCollection<HostSystemInformation>();





最后在AddSystemInformationInIpTable中:



Finally in AddSystemInformationInIpTable:

SystemInformation.Add(new HostSystemInformation
{
    Sno = num++,
    strIpAddr = txtIpAddress,
    strSystemName = hostname,
    strStatus = "connected"
 
});
// no need to call property changed explictly
// OnPropertyChanged(() => SystemInformation);





您已经将_HostSystemInformation声明为ObservableCollection,但它并未在任何地方使用。这是您应该如何公开更改的集合。



You have already declared _HostSystemInformation as ObservableCollection but it's not used anywhere. This is how you should expose collections that change.


这篇关于仅当单击表的标题时才显示DataGrid表值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆