用纯 C# 代码编写的自定义 DataTemplate 的问题

发布于 2024-10-30 20:26:46 字数 1793 浏览 7 评论 0原文

我有这段 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

り繁华旳梦境 2024-11-06 20:26:46

我认为这是一个具有约束力的问题。您需要设置绑定的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文