c#WPF运行时添加绑定到列表的行<;类别>;
我有一个datagrid,现在有2列。“应用程序名称”和“应用程序位置”。起初没有行,只有两列,但当我单击一个按钮并打开一个OpenFileDialog时,我选择.exe文件,然后我希望.exe文件名和位置(路径)在我的datagrid中 起初我设法做到了这一点,但当我双击该行时,它给我的错误是“'EditItem'不允许用于此视图。”。所以我搜索了它,发现我必须将该行绑定到一个列表。我这样做了,现在当我添加一个.exe文件时,行中没有显示任何内容 我的项目类c#WPF运行时添加绑定到列表的行<;类别>;,c#,wpf,binding,C#,Wpf,Binding,我有一个datagrid,现在有2列。“应用程序名称”和“应用程序位置”。起初没有行,只有两列,但当我单击一个按钮并打开一个OpenFileDialog时,我选择.exe文件,然后我希望.exe文件名和位置(路径)在我的datagrid中 起初我设法做到了这一点,但当我双击该行时,它给我的错误是“'EditItem'不允许用于此视图。”。所以我搜索了它,发现我必须将该行绑定到一个列表。我这样做了,现在当我添加一个.exe文件时,行中没有显示任何内容 我的项目类 public class Appl
public class ApplicationItem
{
//public bool isAppRun { get; set; }
public string appName { get; set; }
public string appLocation { get; set; }
}
我的第一个代码用于添加,但在双击行时出错
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.DefaultExt = ".exe";
fileDialog.Filter = "Exe Files (*.exe)|*.exe";
Nullable<bool> selected = fileDialog.ShowDialog();
if (selected == true) //selected a file
{
fileName = fileDialog.SafeFileName.ToString();
filePath = fileDialog.FileName.ToString();
applications_grd.Items.Add(new ApplicationItem()
{
appName = fileName.Remove(fileName.Length - 4),
appLocation = filePath
});
OpenFileDialog fileDialog=新建OpenFileDialog();
fileDialog.DefaultExt=“.exe”;
fileDialog.Filter=“Exe文件(*.Exe)|*.Exe”;
Nullable selected=fileDialog.ShowDialog();
if(selected==true)//选择了一个文件
{
fileName=fileDialog.SafeFileName.ToString();
filePath=fileDialog.FileName.ToString();
applications\u grd.Items.Add(新ApplicationItem()
{
appName=fileName.Remove(fileName.Length-4),
appLocation=filePath
});
我使用列表的第二个代码行中没有文本
string fileName = "";
string filePath = "";
List<ApplicationItem> appItems = new List<ApplicationItem>();
appItems.Add(new ApplicationItem()
{
appName = fileName.Remove(fileName.Length - 4),
appLocation = filePath
});
applications_grd.Items.Add(appItems);
stringfilename=”“;
字符串filePath=“”;
List appItems=新列表();
appItems.Add(新的ApplicationItem()
{
appName=fileName.Remove(fileName.Length-4),
appLocation=filePath
});
应用程序_grd.Items.Add(appItems);
提前感谢。任何帮助都将不胜感激。这可能适用于
xaml
页面添加
AutoGenerateColumns=“True”
网格内和.cs代码集内
applications_grd.ItemsSource = appItems;
这可能适用于
xaml
页面添加
AutoGenerateColumns=“True”
网格内和.cs代码集内
applications_grd.ItemsSource = appItems;
我想我已经让它工作了,但是有一个新问题。我的网格看起来像这个应用程序| Location | appName | appLocation xx.exe D:\xxx\xx xx xx.exe D:\xxx\xx我不知道为什么,但是我的变量appName和appLocation在我的数据网格中。你能解释这个问题吗?这是因为你用网格绑定了列表,你能告诉我吗grid I中的预期结果应该只有2列(应用程序名称和应用程序位置),但用于存储值的变量也在grid中。set AutoGenerateColumns=“False”因为您正在定义列,我想我已经让它工作了,但是有一个新问题。我的网格看起来像这个应用程序| Location | appName | appLocation xx.exe D:\xxx\xx xx.exe D:\xxx\xx我不知道为什么,但是我的变量appName和appLocation在我的数据网格中。您能解释这个问题吗?这是因为您在带有网格的列表中,您能告诉我网格中的预期结果吗?我应该只有2列(应用程序名称和应用程序位置),但用于存储值的变量也在网格中。set AutoGenerateColumns=“False”因为您正在定义列