在 C# 中构建数据模板
我正在尝试在 C# 中构建以下 DataTemplate
<DataTemplate x:Key="lbl">
<!-- Grid 2x2 with black border -->
<Border BorderBrush="Black">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<!-- x-coordinate -->
<TextBlock Text="X=" />
<TextBlock Grid.Column="1" Text="{Binding Path=[XValues], Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}"/>
<!-- y-coordinate -->
<TextBlock Grid.Row="1" Text="Y=" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Value, Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}" />
</Grid>
</Border>
</DataTemplate>
到目前为止,我无法弄清楚 Grid.ColumnDefinitions ,并且出现以下异常
FrameworkElementFactory 必须位于密封模板中才能执行此操作。
private static DataTemplate GetToolTipsDataTemplate()
{
FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid));
FrameworkElementFactory x = new FrameworkElementFactory(typeof(TextBlock));
x.SetBinding(TextBlock.TextProperty, new Binding("X="));
grid.AppendChild(x);
FrameworkElementFactory xValue = new FrameworkElementFactory(typeof(TextBlock));
xValue.SetValue(TextBlock.TextProperty, "{Binding Path=[XValues], Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}");
grid.AppendChild(xValue);
FrameworkElementFactory y = new FrameworkElementFactory(typeof(TextBlock));
y.SetBinding(TextBlock.TextProperty, new Binding("Y="));
grid.AppendChild(y);
FrameworkElementFactory yValue = new FrameworkElementFactory(typeof(TextBlock));
yValue.SetValue(TextBlock.TextProperty, "{Binding Path=Values, Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}");
grid.AppendChild(yValue);
FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
border.SetValue(Border.BorderBrushProperty, System.Windows.Media.Brushes.Black);
border.AppendChild(grid);
DataTemplate dt = new DataTemplate {VisualTree = border};
return dt;
}
任何帮助将不胜感激。
I am trying to build the following DataTemplate in C#
<DataTemplate x:Key="lbl">
<!-- Grid 2x2 with black border -->
<Border BorderBrush="Black">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<!-- x-coordinate -->
<TextBlock Text="X=" />
<TextBlock Grid.Column="1" Text="{Binding Path=[XValues], Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}"/>
<!-- y-coordinate -->
<TextBlock Grid.Row="1" Text="Y=" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Value, Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}" />
</Grid>
</Border>
</DataTemplate>
I have gotten this far and I can't figure out the Grid.ColumnDefinitions
and I am getting an exception of
FrameworkElementFactory must be in a sealed template for this operation.
private static DataTemplate GetToolTipsDataTemplate()
{
FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid));
FrameworkElementFactory x = new FrameworkElementFactory(typeof(TextBlock));
x.SetBinding(TextBlock.TextProperty, new Binding("X="));
grid.AppendChild(x);
FrameworkElementFactory xValue = new FrameworkElementFactory(typeof(TextBlock));
xValue.SetValue(TextBlock.TextProperty, "{Binding Path=[XValues], Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}");
grid.AppendChild(xValue);
FrameworkElementFactory y = new FrameworkElementFactory(typeof(TextBlock));
y.SetBinding(TextBlock.TextProperty, new Binding("Y="));
grid.AppendChild(y);
FrameworkElementFactory yValue = new FrameworkElementFactory(typeof(TextBlock));
yValue.SetValue(TextBlock.TextProperty, "{Binding Path=Values, Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}");
grid.AppendChild(yValue);
FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
border.SetValue(Border.BorderBrushProperty, System.Windows.Media.Brushes.Black);
border.AppendChild(grid);
DataTemplate dt = new DataTemplate {VisualTree = border};
return dt;
}
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 http:// Social.msdn.microsoft.com/Forums/en-US/wpf/thread/289e0000-a3f7-440b-8235-c8ca36320dd4 您可以将行和列作为子项插入。
According to http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/289e0000-a3f7-440b-8235-c8ca36320dd4 you can insert Rows and Columns as children.
我走了一个不同的方向,除了装订上的格式之外,让它正常工作。
I have gone a different direction and gotten it to work except for the formatting on the binding.