Silverlight 4.0:如何在代码后面获取DatagridTemplateColumn标题

发布于 2024-12-05 19:07:13 字数 2689 浏览 0 评论 0原文

在 XAML 中有一个 datagridtemplate 列,

<sdk:DataGridTemplateColumn x:Name="Urgency">
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                            <Image Source="{Binding UrgencyUri}" 
                            VerticalAlignment="Stretch"
                            Stretch="UniformToFill"
                            Height="10" Width="10" Margin="0,0,5,0"/>
                            <TextBlock Text="{Binding Urgency}"/>
                        </StackPanel>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
                <sdk:DataGridTemplateColumn.HeaderStyle>
                    <Style TargetType="primitive:DataGridColumnHeader">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <Grid HorizontalAlignment="Stretch">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="{Binding Path=Urgency, Source={StaticResource MHVWindowResources}}" Grid.Column="0" HorizontalAlignment="Left" />
                                        <filter:DataGridColumnFilter Grid.Column="1" HorizontalAlignment="Right" />
                                    </Grid>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </sdk:DataGridTemplateColumn.HeaderStyle>
            </sdk:DataGridTemplateColumn>

我使用了下面的代码,但它返回 null

DataGridTemplateColumn templateColumn = dataGrid.Columns[columnIndex] as DataGridTemplateColumn;
            if (templateColumn != null)
            {

                string header = templateColumn.Header as string;

                if (header == null)
                {
                    return null;
                }
                bindingPath = header;
            }

他们有什么我错过的吗?谢谢 我想知道如何在代码后面获取列标题。

have in XAML a datagridtemplate column

<sdk:DataGridTemplateColumn x:Name="Urgency">
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                            <Image Source="{Binding UrgencyUri}" 
                            VerticalAlignment="Stretch"
                            Stretch="UniformToFill"
                            Height="10" Width="10" Margin="0,0,5,0"/>
                            <TextBlock Text="{Binding Urgency}"/>
                        </StackPanel>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
                <sdk:DataGridTemplateColumn.HeaderStyle>
                    <Style TargetType="primitive:DataGridColumnHeader">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <Grid HorizontalAlignment="Stretch">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="{Binding Path=Urgency, Source={StaticResource MHVWindowResources}}" Grid.Column="0" HorizontalAlignment="Left" />
                                        <filter:DataGridColumnFilter Grid.Column="1" HorizontalAlignment="Right" />
                                    </Grid>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </sdk:DataGridTemplateColumn.HeaderStyle>
            </sdk:DataGridTemplateColumn>

I used this code below however it returns null

DataGridTemplateColumn templateColumn = dataGrid.Columns[columnIndex] as DataGridTemplateColumn;
            if (templateColumn != null)
            {

                string header = templateColumn.Header as string;

                if (header == null)
                {
                    return null;
                }
                bindingPath = header;
            }

Is their anything that I missed out? Thanks
I want to know how to get the column header in code behind.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

时光倒影 2024-12-12 19:07:13

添加此行:

 var header = templateColumn.Header

在此处放置断点调试并检查快速监视中的类型。

我敢打赌,标题可能永远不是字符串,因此当您使用时它始终为空:

string header = templateColumn.Header as string;

希望有帮助

Add this line:

 var header = templateColumn.Header

place a break point here debug and check what the type is in the quick watch.

My bet is the header is probably never a string so it always null when you use:

string header = templateColumn.Header as string;

hope that helps

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