用纯 C# 代码编写的自定义 DataTemplate 的问题
我有这段 XAML 代码:
<DataTemplate x:Key="detailsCellTemplate">
<StackPanel>
<TextBlock Padding="3, 5, 3, 2" HorizontalAlignment="Left" FontWeight="DemiBold">
<TextBlock.Text>
<Binding Path="client_title" />
</TextBlock.Text>
</TextBlock>
<TextBlock Padding="3, 0, 3, 5" HorizontalAlignment="Left">
<TextBlock.Text>
<Binding Path="client_subtitle" />
</TextBlock.Text>
</TextBlock>
</StackPanel>
</DataTemplate>
它代表 GridColumn 单元格的 DataTemplate,以这种方式(或多或少)翻译成纯 C# 代码:
FrameworkElementFactory stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Vertical);
FrameworkElementFactory title = new FrameworkElementFactory(typeof(TextBlock));
title.SetBinding(TextBlock.TextProperty, new Binding("client_title"));
title.SetValue(TextBlock.ForegroundProperty, Brushes.Black);
title.SetValue(TextBlock.VisibilityProperty, Visibility.Visible);
stackPanelFactory.AppendChild(title);
FrameworkElementFactory subTitle = new FrameworkElementFactory(typeof(TextBlock));
title.SetBinding(TextBlock.TextProperty, new Binding("client_subtitle"));
stackPanelFactory.AppendChild(subTitle);
VisualTree = stackPanelFactory;
并使用以下指令分配给 ListView 控件: gridColumn.CellTemplate = new TitleCellTemplate();
,在之前添加的从 mysql 数据集中提取的 DataContext 上。 数据集与 ListView 绑定没有错误,但网格中的所有行都是白色的,就像它们没有样式或其他东西一样。
哪里可能有错误?
I've this piece of XAML code:
<DataTemplate x:Key="detailsCellTemplate">
<StackPanel>
<TextBlock Padding="3, 5, 3, 2" HorizontalAlignment="Left" FontWeight="DemiBold">
<TextBlock.Text>
<Binding Path="client_title" />
</TextBlock.Text>
</TextBlock>
<TextBlock Padding="3, 0, 3, 5" HorizontalAlignment="Left">
<TextBlock.Text>
<Binding Path="client_subtitle" />
</TextBlock.Text>
</TextBlock>
</StackPanel>
</DataTemplate>
that represents a DataTemplate for a GridColumn Cell, translated in pure C# code (more or less) in this way:
FrameworkElementFactory stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Vertical);
FrameworkElementFactory title = new FrameworkElementFactory(typeof(TextBlock));
title.SetBinding(TextBlock.TextProperty, new Binding("client_title"));
title.SetValue(TextBlock.ForegroundProperty, Brushes.Black);
title.SetValue(TextBlock.VisibilityProperty, Visibility.Visible);
stackPanelFactory.AppendChild(title);
FrameworkElementFactory subTitle = new FrameworkElementFactory(typeof(TextBlock));
title.SetBinding(TextBlock.TextProperty, new Binding("client_subtitle"));
stackPanelFactory.AppendChild(subTitle);
VisualTree = stackPanelFactory;
and assigned to a ListView control with this instruction: gridColumn.CellTemplate = new TitleCellTemplate();
, on a previously added DataContext extracted from a mysql dataset.
The Dataset binds without errors with the ListView, but all the rows in the grid are white, like they don't have a style or something.
Where could be the mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是一个具有约束力的问题。您需要设置绑定的
Source
属性。请参阅 MSDN 上的在代码中创建绑定。
I think this is a binding issue. You need to set the
Source
property of your bindings.See Creating a binding in code on MSDN.