How can I create as many of the same data binding lists as possible, but output different values from the same field name?
Hopefully this question will become clearer with an example. Essentially I have a class of steel corners (and I need to know their width and thickness). I want to load them from .csv and create as many as needed for k <count.
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 for
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 behind this significantly loads each corner with all possible eID angles or all possible changes in angular dimensions. This actually does what I want, except that the datagrid has as many lists as there are eIDs, or eID.Counts.
The second example should clarify 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 that basically one "template" corner is created, each of which has all possible options and adds them based on k <TWN
This will generate the same number of lists, but will prove to be problematic when trying to pull the SelectedValue out of them.
How do I find an intermediate solution?
source to share
Your problem is bad design - u must be using MVVM pattern.
But simple solution: 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>
FROM#
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 it Angles
class
has new Property
storage SelectedItem
.
Anyway check out this Link
source to share