【问题标题】:How to edit CellTemplate in Expression Blend 4如何在 Expression Blend 4 中编辑 CellTemplate
【发布时间】:2012-04-11 05:17:32
【问题描述】:

关于在 Expression Blend 4 中编辑单元格模板,有一点我不明白:

当我通过 xaml 手动创建单元格模板时,代码如下所示:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:leartWPF" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
    x:Class="leartWPF.Window1"
    x:Name="Window"
    Title="Window1"
    Width="640" Height="480">
    <Window.Resources>
        <local:GroupDataSource x:Key="GroupDataSourceDataSource" d:IsDataSource="True"/>

    </Window.Resources>

    <Grid x:Name="LayoutRoot" Margin="0" DataContext="{Binding Source={StaticResource GroupDataSourceDataSource}}">
        <DataGrid  Margin="0" HeadersVisibility="None" ItemsSource="{Binding GroupExtednenDatas, ElementName=Window}" AutoGenerateColumns="False" RowHeight="25" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" CanUserAddRows="False" >
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Groups" Width="Auto" IsReadOnly="True" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                             <TextBlock Text="{Binding Name}" Width="200"></TextBlock>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>     
    </Grid>
</Window>

这会产生一个看起来或多或少没问题的窗口:

当我尝试使用 Expression Blend 的菜单做同样的事情时,

我最终得到以下代码:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:leartWPF" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
    x:Class="leartWPF.Window1"
    x:Name="Window"
    Title="Window1"
    Width="640" Height="480">
    <Window.Resources>
        <local:GroupDataSource x:Key="GroupDataSourceDataSource" d:IsDataSource="True"/>
        <DataTemplate x:Key="MyDataTemplate">
            <TextBlock Text="{Binding Name}" Width="200"></TextBlock>
        </DataTemplate>

    </Window.Resources>

    <Grid x:Name="LayoutRoot" Margin="0" DataContext="{Binding Source={StaticResource GroupDataSourceDataSource}}">
        <DataGrid  Margin="0" HeadersVisibility="None" ItemsSource="{Binding GroupExtednenDatas, ElementName=Window}" AutoGenerateColumns="False" RowHeight="25" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" CanUserAddRows="False" >
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Groups" Width="Auto" IsReadOnly="True" CellTemplate="{DynamicResource MyDataTemplate}" >

                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>     
    </Grid>
</Window>

无论我在里面放什么,它都会显示空单元格

<DataTemplate x:Key="MyDataTemplate"> :

我做错了什么,还是这个 Expression Blend 错误?如果这是一个错误,我应该如何更改 XAML 代码来修复它?

【问题讨论】:

  • 据我所知,它们仅在模板的声明方式上有所不同:前者内联,后者在资源中。你确定你使用的是相同的代码隐藏吗?附: DataGridItemsSource 中是否需要错字?
  • 我实际上重做了几次。另外,我刚刚用 StaticResource 替换了 DynamicResource,它又可以工作了。这里似乎有一个与 DynamicResource 相关的问题

标签: c# wpf xaml wpfdatagrid expression-blend


【解决方案1】:

如果是这样,我可能完全偏离了基础,很抱歉不理解,但似乎这个问题过于复杂了。 CellTemplate 只是任何东西的占位符 - 在数据网格中,默认情况下它是空的,因此您可以将任何您想要的内容放入其中 -> 在您的情况下,它是 TextBlock 的占位符。这就是您需要在 Blend 中编辑的内容,或者您​​在 CellTemplate 中放置的任何其他控件。

【讨论】:

  • 我已将问题范围缩小到引用模板时 DynamicResource 不起作用。该问题与作为 StaticResource 的链接一起正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多