Я плохо использую WPF, на самом деле я использую его впервые. Вот почему мне нужна ваша помощь.
У меня есть DataGrid с AutoGeneratingColumn, и я хочу создать некоторый стиль для dataGrid. Но трудно придать этому стиль. И у меня есть флажок, который при проверке генерирует строку с данными stati c.
Это мой код XAML
<DataGrid x:Name="dtgResults" ColumnWidth="*" AutoGeneratingColumn="dataGrid1_AutoGeneratingColumn">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight"
Value="Bold" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
<CheckBox x:Name="chkMistUserDefined" Grid.Column="0" HorizontalAlignment="Right" Click="chkMistUserDefined_Click"/>
<TextBlock Text="Override predicted misfraction" Grid.Column="1" HorizontalAlignment="Left"/>
Это мой код .cs
public partial class InletPiping : UserControl
{
private string vesselID;
Project mySepProject;
MySepBLL mySepBLL = new MySepBLL();
private object cell;
public virtual System.Drawing.Color BackColor { get; set; }
public InletPiping(string vesselid)
{
vesselID = vesselid;
mySepProject = (Project)App.Current.Properties["myp_projects"];
InitializeComponent();
Vessel vessel = mySepBLL.GetVessel(vesselID, mySepProject.Vessels);
LoadInletPipingData(vessel.Inlets);
LoadResultsComboBox(vessel.Inlets);
}
public void ReloadInletPiping()
{
mySepProject = (Project)App.Current.Properties["myp_projects"];
Vessel vessel = mySepBLL.GetVessel(vesselID, mySepProject.Vessels);
LoadInletPipingData(vessel.Inlets);
LoadResultsComboBox(vessel.Inlets);
}
private void LoadResultsComboBox(List<Inlet> inlets)
{
cmbResults.Items.Clear();
foreach (Inlet inlet in inlets)
{
if (inlet.IsActive)
{
ComboBoxItem cmbitm = new ComboBoxItem();
cmbitm.Content = "Inlet pipe " + inlet.TypeNo;
cmbitm.Tag = inlet;
cmbResults.Items.Add(cmbitm);
}
}
cmbResults.SelectedIndex = 0;
}
private void LoadInletPipingResults(Inlet inlet)
{
DataTable dt = new DataTable();
DataColumn description = new DataColumn("Description", typeof(string));
DataColumn unit = new DataColumn("Unit", typeof(string));
DataColumn case1 = new DataColumn("Case 1", typeof(string));
DataColumn case2 = new DataColumn("Case 2", typeof(string));
DataColumn case3 = new DataColumn("Case 3", typeof(string));
DataColumn case4 = new DataColumn("Case 4", typeof(string));
DataColumn case5 = new DataColumn("Case 5", typeof(string));
DataColumn case6 = new DataColumn("Case 6", typeof(string));
DataColumn max = new DataColumn("Max", typeof(string));
dt.Columns.Add(description);
dt.Columns.Add(unit);
dt.Columns.Add(case1);
dt.Columns.Add(case2);
dt.Columns.Add(case3);
dt.Columns.Add(case4);
dt.Columns.Add(case5);
dt.Columns.Add(case6);
dt.Columns.Add(max);
bool isDisplayMistUserDefined = chkMistUserDefined.IsChecked == true ? true : false;
// bool isDisplayMaxUserDefined = chkMaxUserDefined.IsChecked == true ? true : false;
List<string> cmistfractionP = new List<string>();
List<string> cmistflowrate = new List<string>();
List<string> cmistfractionU = new List<string>();
cmistfractionP.Add("Mist fraction - predicted");
cmistfractionP.Add("%");
cmistfractionU.Add("Mist fraction - user defined");
cmistfractionU.Add("");
cmistflowrate.Add("Mist flow rate");
cmistflowrate.Add("m/hr");
double maxmistflowrate = 0;
double maxmistfraction = 0;
for (int i = 0; i < inlet.ProcessDataUnits.ProcessDataCaseInputs.Count; i++)
{
if (inlet.ProcessDataUnits.ProcessDataCaseInputs[i].IsActive)
{
PipingCalculation pipingcalc = new PipingCalculation(inlet.ProcessDataUnits.ProcessDataCaseInputs[i].GasFlowRate.Value, inlet.ProcessDataUnits.ProcessDataCaseInputs[i].GasDensity.Value,
inlet.ProcessDataUnits.ProcessDataCaseInputs[i].HCLiquidFlowRate.Value, inlet.InletPiping.ActualInsideDiameter.Value);
maxmistfraction = pipingcalc.MistFractionUserDefined > maxmistfraction ? pipingcalc.MistFractionUserDefined : maxmistfraction;
maxmistflowrate = pipingcalc.MistFlowRate > maxmistflowrate ? pipingcalc.MistFlowRate : maxmistflowrate;
cmistfractionP.Add(Double.IsNaN(pipingcalc.MistFractionUserDefined) ? "" : pipingcalc.MistFractionUserDefined.ToString());
cmistfractionU.Add(Double.IsNaN(pipingcalc.MistFractionUserDefined) ? "" : pipingcalc.MistFractionUserDefined.ToString());
cmistflowrate.Add(Double.IsNaN(pipingcalc.MistFlowRate) ? "" : pipingcalc.MistFlowRate.ToString());
}
else
{
cmistfractionP.Add("");
cmistfractionU.Add("");
cmistflowrate.Add("");
}
}
cmistfractionP.Add(Double.IsNaN(maxmistfraction) ? "" : maxmistfraction.ToString());
cmistfractionU.Add(Double.IsNaN(maxmistfraction) ? "" : maxmistfraction.ToString());
cmistflowrate.Add(Double.IsNaN(maxmistflowrate) ? "" : maxmistflowrate.ToString());
dt.Rows.Add(cmistfractionP.ToArray());
if (isDisplayMistUserDefined)
dt.Rows.Add(cmistfractionU.ToArray());
dt.Rows.Add(cmistflowrate.ToArray());
dtgResults.ItemsSource = dt.DefaultView;
}
private double GetMistFractionCalculation(double gasflowrate, double gasdensity, double hcliquidflowrate, double actualInsideDiameter)
{
double mistfraction = 0;
double gasflowrateOvergasdensity = (gasflowrate / gasdensity) / 3600; // if m3/hr = gasflowrate/3600
hcliquidflowrate = hcliquidflowrate / 3600;
actualInsideDiameter = actualInsideDiameter / 1000;
double partialcalc = gasflowrateOvergasdensity / (gasflowrateOvergasdensity + hcliquidflowrate);
double partialmul = 1 / actualInsideDiameter;
mistfraction = partialcalc * partialmul;
double hcliquid = hcliquidflowrate;
double mistflowrate = mistfraction * hcliquid;
mistfraction = mistfraction * 100;
mistflowrate = mistflowrate * 3600;
return mistfraction;
}
private void LoadInletPipingData(List <Inlet> inlets)
{
DataTable dt = new DataTable();
DataColumn inletno = new DataColumn("Inlet pipe #", typeof(string));
DataColumn pipeSize = new DataColumn("Pipe size (inch)", typeof(string));
DataColumn actualID = new DataColumn("Actual ID (mm)", typeof(string));
DataColumn roughness = new DataColumn("Roughness (mm)", typeof(string));
dt.Columns.Add(inletno);
dt.Columns.Add(pipeSize);
dt.Columns.Add(actualID);
dt.Columns.Add(roughness);
foreach (Inlet inlet in inlets)
{
if (inlet.IsActive)
dt.Rows.Add("Inlet Pipe " + inlet.TypeNo, inlet.InletPiping.Size.Value, inlet.InletPiping.ActualInsideDiameter.Value, inlet.InletPiping.Roughness.Value);
}
dtgSettings.Items.Refresh();
dtgSettings.ItemsSource = dt.DefaultView;
}
private void cmbResults_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ReloadResultsDataGrid();
}
private void chkMistUserDefined_Click(object sender, RoutedEventArgs e)
{
ReloadResultsDataGrid();
}
private void chkMaxUserDefined_Click(object sender, RoutedEventArgs e)
{
ReloadResultsDataGrid();
}
private void ReloadResultsDataGrid()
{
ComboBoxItem cmb = (ComboBoxItem)cmbResults.SelectedItem;
if (cmb != null)
{
Inlet inlet = (Inlet)cmb.Tag;
LoadInletPipingResults(inlet);
}
}
private void TabItem_GotFocus(object sender, RoutedEventArgs e)
{
}
private void dtgSettings_CurrentCellChanged(object sender, EventArgs e)
{
string myPoint = dtgSettings.SelectedIndex.ToString() + dtgSettings.SelectedValue.ToString();
}
private void dtgSettings_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.EditAction == DataGridEditAction.Commit)
{
var column = e.Column as DataGridBoundColumn;
if (column != null)
{
mySepProject = (Project)App.Current.Properties["myp_projects"];
Vessel vessel = mySepBLL.GetVessel(vesselID, mySepProject.Vessels);
var bindingPath = (column.Binding as Binding).Path.Path;
if (bindingPath == "Actual ID (mm)")
{
int idx = e.Row.GetIndex();
var el = e.EditingElement as TextBox;
vessel.Inlets[idx].InletPiping.ActualInsideDiameter.Value = Convert.ToDouble(el.Text);
vessel.Inlets[idx].InletPiping.ActualInsideDiameter.IsOverridden = true;
}
else if (bindingPath == "Pipe size (inch)")
{
int idx = e.Row.GetIndex();
var el = e.EditingElement as TextBox;
vessel.Inlets[idx].InletPiping.Size.Value = Convert.ToDouble(el.Text);
vessel.Inlets[idx].InletPiping.Size.IsOverridden = true;
if (!vessel.Inlets[idx].InletPiping.ActualInsideDiameter.IsOverridden)
{
vessel.Inlets[idx].InletPiping.ActualInsideDiameter.Value = vessel.Inlets[idx].InletPiping.Size.Value * 25.4;
DataRowView rowView = e.Row.Item as DataRowView;
rowView["Actual ID (mm)"] = vessel.Inlets[idx].InletPiping.ActualInsideDiameter.Value;
}
}
else if (bindingPath == "Roughness (mm)")
{
int idx = e.Row.GetIndex();
var el = e.EditingElement as TextBox;
vessel.Inlets[idx].InletPiping.Roughness.Value = Convert.ToDouble(el.Text);
}
App.Current.Properties["myp_projects"] = mySepProject;
ReloadResultsDataGrid();
}
}
}
И это результат моего кода
And this is what I need to do
введите описание изображения здесь
Примечание: серые ячейки нельзя редактировать