Problem with a custom DataTemplate written in pure C# code
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?
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.
精彩评论