【发布时间】:2010-09-22 20:03:25
【问题描述】:
我正在尝试在 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;
}
任何帮助将不胜感激。
【问题讨论】:
标签: c# wpf binding datatemplate frameworkelementfactory