Reputation: 63
I hope this question becomes clearer with the example. Essentially, I have a class of steel angles (and I need to know their widths and thicknesses). I want to load them from a .csv, and create as many as needed for k < count.
The first example:
XAML
<DataGridTemplateColumn x:Name="cBoxes" Header="Size" Width="*" CanUserReorder="False" CanUserSort="False" CanUserResize="False" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="testBox" Margin="2" ItemsSource="{Binding eID}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Code Behind
public class Angles
{
public double TYPE { get; set; }
public string eID { get; set; }
public double WIDTH { get; set; }
public ObservableCollection<double> THICKNESS { get; set; }
}
private ObservableCollection<Angles> angleDb = new ObservableCollection<Angles>();
private ObservableCollection<string> angleIDStrings = new ObservableCollection<string>();
public void LoadAngles()
{
var wn = Convert.ToDouble(textboxWebNumber.Text);
var twn = wn % 2 == .5 ? wn + (wn / 2 - .5) + 6 : wn + (Math.Floor(wn / 2)) + 6;
var csvColl = (from line in File.ReadAllLines("angledb.csv").Skip(1)
let parts = line.Split(',')
select new
{
Width = Convert.ToDouble(parts[0]),
Thickness = Convert.ToDouble(parts[1])
}).ToList();
foreach (var item in csvColl)
{
var content = $"L{item.Width}x{item.Width}x{item.Thickness}";
angleIDStrings.Add(content);
angleDb.Add(new Angles() {
THICKNESS = item.Thickness, WIDTH = item.Width, eID = angleIDStrings });
}
DataContext = angleDb;
}
The logic of this essentially loads each angle with all possible angle eIDs, or all possible variations of the angle dimensions. This actually does what I want, with the exception that the datagrid is populated with as many comboboxes as there are eIDs, or eID.Count.
The second example should make clearer what I want:
public class Angles
{
public double TYPE { get; set; }
public ObservableCollection<string> eID { get; set; }
public ObservableCollection<double> WIDTH { get; set; }
public ObservableCollection<double> THICKNESS { get; set; }
} // modify the class, changing all fields to observablecollections
private ObservableCollection<double> angleWidths = new ObservableCollection<double>();
private ObservableCollection<double> angleThicknesses = new ObservableCollection<double>();
public List<Angles> finalList = new List<Angles>();
public void LoadAngles()
{
var wn = Convert.ToDouble(textboxWebNumber.Text);
var twn = wn % 2 == .5 ? wn + (wn / 2 - .5) + 6 : wn + (Math.Floor(wn / 2)) + 6;
// Read each line, break it at commas, assign split-elements, save into a list.
var csvColl = (from line in File.ReadAllLines("angledb.csv").Skip(1)
let parts = line.Split(',')
select new
{
Width = Convert.ToDouble(parts[0]),
Thickness = Convert.ToDouble(parts[1])
}).ToList();
foreach (var item in csvColl)
{
var content = $"L{item.Width}x{item.Width}x{item.Thickness}";
angleIDStrings.Add(content);
angleThicknesses.Add(item.Thickness);
angleWidths.Add(item.Width);
}
for (var k = 0; k < twn; k++)
{
angleDb.Add(new Angles
{
THICKNESS = angleThicknesses,
WIDTH = angleWidths,
eID = angleIDStrings
});
}
DataContext = angleDb;
}
The logic here is essentially setting up one "template" angle, each which have all possible variations, and adding them based on k < twn
This will generate as many comboboxes, but will prove problematic when trying to pull the SelectedValue from them.
How can I find the in-between solution?
Upvotes: 4
Views: 50
Reputation: 1687
Your problem is bad design - u should use MVVM pattern.
But the simple solution is: XAML
<DataGridTemplateColumn x:Name="cBoxes" Header="Size" Width="*" CanUserReorder="False" CanUserSort="False" CanUserResize="False" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="testBox" Margin="2" SelectedItem="{Binding SelectedEID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding eID}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
C#
public class Angles
{
public double TYPE { get; set; }
public string SelectedEID { get; set; }
public ObservableCollection<string> eID { get; set; }
public ObservableCollection<double> WIDTH { get; set; }
public ObservableCollection<double> THICKNESS { get; set; }
}
Obviously the Angles
class
has now a new Property
to hold the SelectedItem
.
Anyway check out this Link
Upvotes: 1