C# 列表框未按预期更新
我正在使用与Windows Phone相同的代码,但现在在WPF桌面应用程序中使用它 它读取一个目录,然后读取目录中的每个xml,然后读取xml并将详细信息发布到_item类 它似乎正确地将每个单独的项添加到了_项中(比如它从目录中读取了3个xml),但是列表中填充了三个项,所有这些项都具有相同的细节(从它读取的最后一个xml)。 我做错了什么 C# XAML:C# 列表框未按预期更新,c#,wpf,.net-4.0,desktop-application,C#,Wpf,.net 4.0,Desktop Application,我正在使用与Windows Phone相同的代码,但现在在WPF桌面应用程序中使用它 它读取一个目录,然后读取目录中的每个xml,然后读取xml并将详细信息发布到_item类 它似乎正确地将每个单独的项添加到了_项中(比如它从目录中读取了3个xml),但是列表中填充了三个项,所有这些项都具有相同的细节(从它读取的最后一个xml)。 我做错了什么 C# XAML: 您的foreach每次都添加相同的\u项引用,并在下次循环运行时对其进行修改。结果很可能是最后一份工作显示在所有职位上 将\u项创建
您的
foreach
每次都添加相同的\u项
引用,并在下次循环运行时对其进行修改。结果很可能是最后一份工作显示在所有职位上
将\u项
创建为内部foreach
循环中的局部变量,这样每个项都独立于最后一个:
foreach (string fileNameXML in fileEntries)
{
//read 1 xml at a time.
XmlDocument doc = new XmlDocument();
doc.Load(fileNameXML);
XmlNodeList nodes = doc.DocumentElement.SelectNodes("/Job");
foreach (XmlNode node in nodes)
{
var item = new MyItem(); // Whatever type _item was.
item.Input = node.SelectSingleNode("Input").InnerText;
item.OutputFolder = node.SelectSingleNode("OutputFolder").InnerText;
item.OutputFile = node.SelectSingleNode("OutputFile").InnerText;
item.Format = node.SelectSingleNode("Format").InnerText;
item.Name = node.SelectSingleNode("Name").InnerText;
item.Effects = node.SelectSingleNode("Effect").InnerText;
item.Type = node.SelectSingleNode("Type").InnerText;
item.Output = item.OutputFolder + item.OutputFile + "." + item.Format.ToLower();
lstBoxQueue.Items.Add(item);
}
}
我是看错了,还是三次都是同一个作业?是的,它三次列出同一个作业,即使它在每个不同的作业中正确循环,但在lstBoxQueue.Items.Add(_item)中断时,您可以看到每个作业;Line您在哪里创建_项目?可能是您一直在添加相同的引用吗?我错过了XMLItems _item=new XMLItems();在foreach循环之外(以前)谢谢,就这样。My XMLItems_item=新的XMLItems();就在外边!
<ListBox Name="lstBoxQueue" BorderBrush="{x:Null}" Background="#FFA09F9F" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="80" Width="629" HorizontalAlignment="Left" Background="#FFCDCDCD" >
<Border BorderBrush="Gray" BorderThickness="1"/>
<TextBlock Text="Job name: " Margin="9,10,432,49" FontWeight="Bold" Foreground="#FF323232" FontSize="12" />
<TextBlock Foreground="Black" HorizontalAlignment="Left" MinWidth="50" FontSize="10" TextWrapping="Wrap" Text="{Binding Input}" Margin="76,33,0,0" VerticalAlignment="Top" MaxWidth="270" MaxHeight="15"/>
<TextBlock Foreground="Black" HorizontalAlignment="Left" FontSize="10" TextWrapping="Wrap" Text="{Binding Output}" Margin="77,54,0,0" MinWidth="100" MaxWidth="270" VerticalAlignment="Top" />
<TextBlock Foreground="Black" HorizontalAlignment="Left" MinWidth="50" FontSize="10" TextWrapping="Wrap" Text="{Binding Name}" Margin="76,12,0,0" VerticalAlignment="Top" />
<TextBlock Foreground="Black" HorizontalAlignment="Left" FontSize="10" TextWrapping="Wrap" Text="{Binding Effects}" Margin="374,31,0,0" MinWidth="100" MaxWidth="270" VerticalAlignment="Top" Height="40" />
<TextBlock Foreground="Black" HorizontalAlignment="Left" FontSize="10" TextWrapping="Wrap" Text="{Binding Type}" Margin="374,11,0,0" MinWidth="100" MaxWidth="270" VerticalAlignment="Top" Height="15" />
<Button Content="More Info.." Width="80" Height="25" Margin="538,10,11,45"/>
<Button Content="Cancel Job" Width="80" Height="25" Click="ClickCancelJob" Margin="539,43,10,12"/>
<TextBlock Text="Output: " Margin="25,52,414,7" FontWeight="SemiBold" Foreground="#FF323232"/>
<TextBlock Text="Effects: " Margin="351,29,102,30" FontWeight="SemiBold" Foreground="#FF323232"/>
<TextBlock Text="Input: " Margin="33,30,430,29" FontWeight="SemiBold" Foreground="#FF323232"/>
<TextBlock Text="Export Type: " Margin="318,10,100,49" FontWeight="SemiBold" Foreground="#FF323232"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
class XMLItems
{
//Accessor class for properties of jobs. Enables all pages to access the job properties.
private string _input;
private string _outputFolder;
private string _outputFile;
private string _output;
private string _name;
private string _effects;
private string _type;
private string _format;
public string Input
{
get { return _input; }
set { _input = value; }
}
public string OutputFolder
{
get { return _outputFolder; }
set { _outputFolder = value; }
}
public string OutputFile
{
get { return _outputFile; }
set { _outputFile = value; }
}
public string Output
{
get { return _output; }
set { _output = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Effects
{
get { return _effects; }
set { _effects = value; }
}
public string Type
{
get { return _type; }
set { _type = value; }
}
public string Format
{
get { return _format; }
set { _format = value; }
}
}
foreach (string fileNameXML in fileEntries)
{
//read 1 xml at a time.
XmlDocument doc = new XmlDocument();
doc.Load(fileNameXML);
XmlNodeList nodes = doc.DocumentElement.SelectNodes("/Job");
foreach (XmlNode node in nodes)
{
var item = new MyItem(); // Whatever type _item was.
item.Input = node.SelectSingleNode("Input").InnerText;
item.OutputFolder = node.SelectSingleNode("OutputFolder").InnerText;
item.OutputFile = node.SelectSingleNode("OutputFile").InnerText;
item.Format = node.SelectSingleNode("Format").InnerText;
item.Name = node.SelectSingleNode("Name").InnerText;
item.Effects = node.SelectSingleNode("Effect").InnerText;
item.Type = node.SelectSingleNode("Type").InnerText;
item.Output = item.OutputFolder + item.OutputFile + "." + item.Format.ToLower();
lstBoxQueue.Items.Add(item);
}
}