【问题标题】:Problem with a custom DataTemplate written in pure C# code用纯 C# 代码编写的自定义 DataTemplate 的问题
【发布时间】:2011-08-01 07:46:06
【问题描述】:

我有这段 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 上。 Dataset 与 ListView 绑定没有错误,但网格中的所有行都是白色的,就像它们没有样式或其他东西一样。

可能是哪里出错了?

【问题讨论】:

  • 您确认这不是绑定问题吗?

标签: c# .net wpf binding wpfdatagrid


【解决方案1】:

我认为这是一个有约束力的问题。您需要设置绑定的Source 属性。

请参阅 MSDN 上的 Creating a binding in code

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 2012-10-22
    • 2012-05-09
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    相关资源
    最近更新 更多