Han*_*abi 1 c# wpf datatemplate
我有以下代码:
protected override DataTemplate _CreateDataTemplate()
{
DataTemplate dataTemplate = new DataTemplate();
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(DockPanel));
factory.SetBinding(Panel.BackgroundProperty, new Binding(CellContentBindingPath.Replace(".ValueUser", ".Background")));
dataTemplate.VisualTree = factory;
var childFactory = new FrameworkElementFactory(typeof(Image));
childFactory.SetValue(FrameworkElement.WidthProperty, 15);
factory.AppendChild(childFactory);
childFactory = new FrameworkElementFactory(typeof(TextBlock));
factory.AppendChild(new FrameworkElementFactory(""));
childFactory.SetBinding(TextBlock.TextProperty, !ShowZero ? new Binding(CellContentBindingPath)
{
Converter = new ValueToNothingConverter()
} : new Binding(CellContentBindingPath));
childFactory.SetValue(FrameworkElement.HorizontalAlignmentProperty, ContentAlignment);
factory.AppendChild(childFactory);
return dataTemplate;
}
Run Code Online (Sandbox Code Playgroud)
错误是"15不是属性宽度的有效值".
当我没有设置图像的宽度一切正常.不幸的是,宽度非常重要.
对不起代码格式错误,我没有发现如何使其格式化.
该FrameworkElement.Width属性的类型为double,但您尝试将其设置为整数值.
相反,将其写为以下之一:
childFactory.SetValue(FrameworkElement.WidthProperty, 15.0);
childFactory.SetValue(FrameworkElement.WidthProperty, 15d);
childFactory.SetValue(FrameworkElement.WidthProperty, 15D);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1311 次 |
| 最近记录: |