- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 DataGrid 在 C# 中显示字符串表。列名是动态生成的(即在编译时未知)。
这是一个例子:
在这里,数字已转换为字符串。
我使用 DataTable 作为包含整个表(行和列标题)的 DataGrid 的源。但是,我的问题是“气候变化”列中的值未显示在 DataGrid 中。相反,我在控制台上收到以下错误消息
"BindingExpression path error: 'climate w' property not found on 'object'
''DataRowView' (HashCode=22429696)'. BindingExpression:Path=climate
w/change; DataItem='DataRowView' (HashCode=22429696); target element is
'TextBlock' (Name=''); target property is 'Text' (type 'String')"
public DataTable PaValues { get; set; }
private void CreateDataSet()
{
var dt = new DataTable("Perturbation Analysis");
List<String> ics = _perturbationAnalysis.ImpactCatagories();
dt.Columns.Add("Parameters");
foreach (var ic in ics)
{
dt.Columns.Add(Sanatize(ic));
}
foreach (var parameter in _perturbationAnalysis.ParameterNames())
{
var dr = dt.NewRow();
dr[0] = parameter;
for (int i = 0; i < ics.Count; i++)
{
dr[i+1] = _perturbationAnalysis[parameter, ics[i]].ToString();
}
dt.Rows.Add(dr);
}
PaValues = dt;
}
private string Sanatize(string ic)
{
//return ic.Replace("/", "[/]").Replace("[", "").Replace("]", "").Replace(".", " ");
//return "[" + ic + "]";
return ic;
}
<DataGrid
x:Name="PAGrid"
CanUserAddRows="False"
CanUserDeleteRows="False"
ClipboardCopyMode="IncludeHeader"
FrozenColumnCount="1"
ItemsSource="{Binding Path=PaValues,UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource DataGridStyle}"
IsReadOnly="True">
</DataGrid>
private void PaViewAutoGeneratingColumnHandler(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
e.Column = new DataGridTextColumn
{
Binding = new Binding("[" + e.Column.Header + "]"), Header=e.Column.Header
};
}
BindingExpression path error: '[]' property not found on 'object' ''DataRowView' (HashCode=56876317)'.
BindingExpression:Path=[EDIP2003 w/o LT, acidification w/o LT, acidification w/o LT]
最佳答案
查看关于 PropertyPath syntax 的文档,我们可以看到为什么字符串无法映射。
您已经指出并可以修复的第一个是斜杠 /
:
Source Traversal (Binding to Hierarchies of Collections)
<object Path="propertyName/propertyNameX" .../>
The / in this syntax is used to navigate within a hierarchical data source object, and multiple steps into the hierarchy with successive / characters are supported.
[]
) 来解决。
Multiple Indexers
<object Path="[index1,index2...]" .../>
or
<object Path="propertyName[index,index2...]" .../>
If a given object supports multiple indexers, those indexers can be specified in order, similar to an array referencing syntax. The object in question can be either the current context or the value of a property that contains a multiple index object.
Escapes for Property Path Strings
For certain business objects, you might encounter a case where the property path string requires an escape sequence in order to parse correctly. The need to escape should be rare, because many of these characters have similar naming-interaction issues in languages that would typically be used to define the business object.
- Inside indexers ([ ]), the caret character (^) escapes the next character.
private void PaViewAutoGeneratingColumnHandler(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
var columnName = (string)e.Column.Header;
// We'll build a string with escaped characters.
// The capacity is the length times 2 (for the carets),
// plus 2 for the square brackets.
// This is not optimized for multi-character glyphs, like emojis
var bindingBuilder = new StringBuilder(columnName.Length * 2 + 2);
bindingBuilder.Append('[');
foreach (var c in columnName)
{
bindingBuilder.Append('^');
bindingBuilder.Append(c);
}
bindingBuilder.Append(']');
e.Column = new DataGridTextColumn
{
Binding = new Binding(bindingBuilder.ToString()),
Header = e.Column.Header,
};
}
关于列名中的 C# DataTable DataGrid 特殊字符 "/"(斜杠),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50327749/
我是一名优秀的程序员,十分优秀!