C# 缩短你的代码。生成controlList集合时,不需要检查子元素的各个类型。您已经提供了一个收集所有Control对象的通用收集器。您正在检查的所有元件实际上都属于控制类型。因此,所有if语句都是冗余的(除了过滤所有控件元素的语句)。由于您使用直接转换将子
C# 缩短你的代码。生成controlList集合时,不需要检查子元素的各个类型。您已经提供了一个收集所有Control对象的通用收集器。您正在检查的所有元件实际上都属于控制类型。因此,所有if语句都是冗余的(除了过滤所有控件元素的语句)。由于您使用直接转换将子,c#,wpf,reporting-services,C#,Wpf,Reporting Services,缩短你的代码。生成controlList集合时,不需要检查子元素的各个类型。您已经提供了一个收集所有Control对象的通用收集器。您正在检查的所有元件实际上都属于控制类型。因此,所有if语句都是冗余的(除了过滤所有控件元素的语句)。由于您使用直接转换将子元素转换为控件,因此当元素不是控件(例如面板,如网格或堆栈面板)时,代码将引发异常。我无法理解您计划如何使用标签计算名称+值对以用作报表参数。名称和值是什么?通常一个标签与另一个控件配对。 private REService.Parameter
缩短你的代码。生成
controlList
集合时,不需要检查子元素的各个类型。您已经提供了一个收集所有Control
对象的通用收集器。您正在检查的所有元件实际上都属于控制类型
。因此,所有if语句都是冗余的(除了过滤所有控件元素的语句)。由于您使用直接转换将子元素转换为控件
,因此当元素不是控件
(例如面板
,如网格
或堆栈面板
)时,代码将引发异常。我无法理解您计划如何使用标签计算名称+值对以用作报表参数。名称和值是什么?通常一个标签与另一个控件配对。
private REService.ParameterValue[] GetReportExecutionParameters()
var controlList = new List<Control>();
foreach (var control in StackPanelParameters.Children.OfType<Object>())
if (control is DatePicker )
{
DatePicker dp = (DatePicker)control;
controlList.Add(dp);
continue;
}
var parameterValues = new List<REService.ParameterValue>();
private REService.ParameterValue[] GetReportExecutionParameters()
{
var controlList = new List<Control>();
//same issue using UIElement....
//https://stackoverflow.com/questions/14688136/loop-through-stackpanel-children-genericaly
//***This one helped:
//https://stackoverflow.com/questions/22298431/traversing-elements-of-stackpanel/22298680#22298680
//create a list<> of controls extracted from the StackPanel, add them to the "controlList"
foreach (var control in StackPanelParameters.Children.OfType<Object>())
{
if (control is DatePicker )
{
DatePicker dp = (DatePicker)control;
controlList.Add(dp);
continue;
}
if (control is TextBox)
{
TextBox t = (TextBox)control;
controlList.Add(t);
continue;
}
if (control is CheckBox)
{
CheckBox cb = (CheckBox)control;
controlList.Add(cb);
continue;
}
if (control is ComboBox)
{
ComboBox cbx = (ComboBox)control;
controlList.Add(cbx);
continue;
}
if (control is Label)
{
Label l = (Label)control;
controlList.Add(l);
continue;
}
else
{
//the generic case
Control x = (Control)control;
controlList.Add(x);
continue;
}
} //Ok, now I have a populated controlList
// now I'm assigning the control value to the report parameter
// per "https://odetocode.com/articles/123.aspx" a different approach?
var parameterValues = new List<REService.ParameterValue>();
foreach(var control in controlList)
{
if (control is DatePicker)
{
parameterValues.Add(new REService.ParameterValue
{
Name = control.Name,
Value = ((DatePicker)control).ToString()
});
continue;
}
else if (control is TextBox)
{
parameterValues.Add(new REService.ParameterValue
{
Name = control.Name,
Value = ((TextBox)control).Text
});
continue;
}
else if (control is CheckBox)
{
parameterValues.Add(new REService.ParameterValue
{
Name = control.Name,
Value = ((CheckBox)control).IsChecked.ToString()
});
continue;
}
else if (control is ComboBox)
{
parameterValues.Add(new REService.ParameterValue
{
Name = control.Name,
Value = ((ComboBox)control).SelectedItem.ToString()
});
continue;
}
else if (control is Label)
{
parameterValues.Add(new REService.ParameterValue
{
//Name = control.ToString(),
//Name = control.Name, //this one is null
//Name = ((System.Windows.Forms.Label)control).label
//the control doesn't have a Label property, where did I lose you, little Label?
Name = "",
Value = ((Label)control).Content.ToString()
});
continue;
}
}
return parameterValues.ToArray();
}