目前我正在开发一个用户界面来获取值,以便处理它们以实现自动化。因此,我使用以下XAML代码创建了一个UserControl:
<UserControl x:Class="DATEVswv_PackageCreator.SetPackageParameters"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DATEVswv_PackageCreator"
mc:Ignorable="d"
d:DesignHeight="1000" d:DesignWidth="900">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="89*"/>
<ColumnDefinition Width="45*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/> [...]
</Grid.RowDefinitions>
<Grid Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2">
<Label Name="lbl_header" Content="Abschnitt 1: Paketdaten erfassen" FontSize="14" FontWeight="Bold" VerticalAlignment="Center"/>
</Grid>
<Grid Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Name="grid_manufacturer">
<Label Name="lbl_step1" Content="Schritt 1: Hersteller auswählen" VerticalAlignment="Top" Margin="0,0,0,10" />
<ComboBox
x:Name="cbx_manufacturer"
HorizontalAlignment="Stretch"
Margin="0,18,0,10"
VerticalAlignment="Center"
SelectionChanged="cbx_manufacturer_SelectionChanged"
IsTextSearchEnabled="True"
IsEditable="True"
/>
</Grid>
</Grid>
</UserControl>
此UserControl的代码隐藏是:
public partial class SetPackageParameters : UserControl
{
public SetPackageParameters()
{
dr.getmanufacturers();
InitializeComponent();
cbx_manufacturer.ItemsSource = DataReader.manufacturercollection;
}
DataReader dr = new DataReader();
bool manufacturercreated = false;
private void cbx_manufacturer_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (grid_tfsid.Visibility == Visibility.Visible || grid_packagetype.Visibility == Visibility.Visible || grid_language.Visibility == Visibility.Visible || grid_version.Visibility == Visibility.Visible || grid_product.Visibility == Visibility.Visible || grid_productowner.Visibility == Visibility.Visible || grid_packagecreator.Visibility == Visibility.Visible || grid_license.Visibility == Visibility.Visible)
{
grid_tfsid.Visibility = Visibility.Hidden;
grid_packagetype.Visibility = Visibility.Hidden;
grid_language.Visibility = Visibility.Hidden;
grid_version.Visibility = Visibility.Hidden;
grid_product.Visibility = Visibility.Hidden;
grid_productowner.Visibility = Visibility.Hidden;
grid_packagecreator.Visibility = Visibility.Hidden;
grid_license.Visibility = Visibility.Hidden;
}
try
{
if (cbx_manufacturer.SelectedValue.ToString() == "Neu" && DataReader.manufacturercollection.Contains(MainWindow.tempstring) == false)
{
manufacturercreated = true;
AddValue add = new AddValue();
add.ShowDialog();
Debug.WriteLine(string.Format("Vor der Bearbeitung hatte die manufacturercollection {0} Einträge.", DataReader.manufacturercollection.Count));
DataReader.manufacturercollection.Add(MainWindow.tempstring);
Debug.WriteLine(string.Format("{0} wurde der Collection manufacturercollection hinzugefügt. Die Collection hat jetzt {1} Einträge.", MainWindow.tempstring, DataReader.manufacturercollection.Count));
Debug.WriteLine(string.Format("Der ausgewählte Index wird auf {0} festgelegt.", DataReader.manufacturercollection.Count - 1));
cbx_manufacturer.SelectedIndex = DataReader.manufacturercollection.Count - 1;
Debug.WriteLine(string.Format("Der ausgewählte Index wurde auf {0} festgelegt.", cbx_manufacturer.SelectedIndex));
}
else
{
if (manufacturercreated)
{
cbx_manufacturer.SelectedIndex = DataReader.manufacturercollection.Count - 1;
}
else
{
MainWindow.tempstring = cbx_manufacturer.SelectedValue.ToString();
}
dr.getproducts(cbx_manufacturer.SelectedValue.ToString());
grid_product.Visibility = Visibility.Visible;
cbx_product.ItemsSource = DataReader.productcollection;
}
}
catch
} { }
}
AddValue类是一个简单的窗口,它将输入的字符串保存到变量MainWindow.tempstring中。
ObservableCollection DataReade.manufacturercollection在DataReader类中填充:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
namespace DATEVswv_PackageCreator
{
class DataReader
{
private string[] paths;
public static List<string> packages = new List<string>();
private static List<string> manufacturers = new List<string>();
private static List<string> products = new List<string>();
private static List<string> versions = new List<string>();
private static List<string> languages = new List<string>();
private static List<string> packagetypes = new List<string>();
private static List<string> tfsids = new List<string>();
public static ObservableCollection<string> manufacturercollection;
public static ObservableCollection<string> productcollection;
public static ObservableCollection<string> versioncollection;
public static ObservableCollection<string> languagecollection;
public static ObservableCollection<string> packagetypecollection;
public static ObservableCollection<string> tfsidcollection;
private void getfoldernames(string path)
{
paths = Directory.GetDirectories(path);
}
private void getfoldernames(string path, string pattern)
{
pattern = "*" + pattern + "*";
paths = Directory.GetDirectories(path, pattern);
}
public void cleanpaths()
{
getfoldernames(Params.path);
packages.Clear();
for (int i = 0; i < paths.Length; i++)
{
paths[i] = paths[i].Remove(0, Params.path.Length + 1);
if (paths[i].StartsWith("_") == false)
{
packages.Add(paths[i]);
}
}
}
public List<string> cleanpaths(string pattern)
{
getfoldernames(Params.path, pattern);
packages.Clear();
for (int i = 0; i < paths.Length; i++)
{
paths[i] = paths[i].Remove(0, Params.path.Length + 1);
if (paths[i].StartsWith("_") == false)
{
packages.Add(paths[i]);
}
}
return packages;
}
public void getmanufacturers()
{
cleanpaths();
manufacturers.Clear();
manufacturers.Add("Neu");
foreach (string item in packages)
{
var temparray = item.Split('_');
manufacturers.Add(temparray[0]);
}
manufacturers = manufacturers.Distinct().ToList();
manufacturercollection = new ObservableCollection<string>(manufacturers);
}
public void getproducts(string manufacturer)
{
packages = cleanpaths(MainWindow.tempstring);
products.Clear();
products.Add("Neu");
foreach (string item in packages)
{
var temparray = item.Split('_');
if (temparray[0] == manufacturer && temparray != null)
{
products.Add(temparray[1]);
}
}
products = products.Distinct().ToList();
productcollection = new ObservableCollection<string>(products);
}
public void getversions(string product)
{
packages = cleanpaths(MainWindow.tempstring);
versions.Clear();
versions.Add("Neu");
foreach (string item in packages)
{
var temparray = item.Split('_');
if (temparray[1] == product && temparray != null)
{
versions.Add(temparray[2]);
}
}
versions = versions.Distinct().ToList();
versioncollection = new ObservableCollection<string>(versions);
}
public void getlanguages(string version)
{
packages = cleanpaths(MainWindow.tempstring);
languages.Clear();
languages.Add("Neu");
foreach (string item in packages)
{
var temparray = item.Split('_');
if (temparray[2] == version && temparray != null)
{
languages.Add(temparray[3]);
}
}
languages = languages.Distinct().ToList();
languagecollection = new ObservableCollection<string>(languages);
}
public void getpackagetypes(string language)
{
packages = cleanpaths(MainWindow.tempstring);
packagetypes.Clear();
packagetypes.Add("Neu");
foreach (string item in packages)
{
var temparray = item.Split('_');
if (temparray[3] == language && temparray != null)
{
packagetypes.Add(temparray[4]);
}
}
packagetypes = packagetypes.Distinct().ToList();
packagetypecollection = new ObservableCollection<string>(packagetypes);
}
public void gettfsids(string packagetype)
{
packages = cleanpaths(MainWindow.tempstring);
tfsids.Clear();
tfsids.Add("Neu");
foreach (string item in packages)
{
var temparray = item.Split('_');
if (temparray[4] == packagetype && temparray != null)
{
tfsids.Add(temparray[5]);
}
}
tfsids = tfsids.Distinct().ToList();
tfsidcollection = new ObservableCollection<string>(tfsids);
}
}
}
Params-class只从xml文件中获取配置值并将它们放入静态变量中。
问题:当我选择项目“Neu”来创建新项目时,它会正确地将该字符串添加到DataReader.manufacturercollection
并说明该值已被选中。但是组合框中的值仍然是“Neu”。对于在类DataReader中创建的所有其他集合,错误是相同的。
我做错了什么?