如何绑定行标题在WPF数据网格? [英] How to bind Row Header in wpf datagrid?

查看:214
本文介绍了如何绑定行标题在WPF数据网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目,我要像Outlook日历DataGrid行头值。对于假设为9.00,9.30,10.00,10.30 ....但它不是固定不变的,它可能会有所不同,.. 9.00点,10.00 PM。

In my project i want datagrid row header values like outlook calendar. For assuming that is 9.00, 9.30, 10.00, 10.30.... But its not fixed one, it may vary,.. 9.00 pm, 10.00 pm.

我是使用两个TextBlock的,这和我得到了大致相同的形状了。但我的价值是固定的一个是1-00,1-00,1-00,1-00,1-00,1-00 ........

i am using two TextBlock for this and i got almost same shape too. but my values are fixed one that is 1-00, 1-00,1-00, 1-00,1-00, 1-00,........

我的代码 -

<DataGrid AutoGenerateColumns="False" Height="560" HorizontalAlignment="Left" Margin="30,54,0,0" Name="myDataGrid" VerticalAlignment="Top" Width="884" MouseDoubleClick="myDataGrid_MouseDoubleClick" IsEnabled="True" SelectionUnit="Cell">
        <DataGrid.RowHeaderTemplate>
            <DataTemplate>
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
                        <TextBlock Text="1" Foreground="#9493CF" FontSize="16" />
                        <TextBlock Text="00" Foreground="#9493CF" />
                    </StackPanel>
            </DataTemplate>
        </DataGrid.RowHeaderTemplate>
            <DataGrid.ContextMenu>
                <ContextMenu x:Name="LeftClickMenu">
                    <MenuItem Header="New Appointment" Click="MenuItem_Click"/>
                    <!--<MenuItem Header="Save"/>
                    <MenuItem Header="Print"/>-->
                    <Separator/>
                    <MenuItem Header="Exit"/>
                </ContextMenu>
            </DataGrid.ContextMenu>
        </DataGrid>



我要什么 -

What I want -

我得到了什么 -

推荐答案

可以指定负上边距在第二个文本块:

<TextBlock Text="00" Foreground="#9493CF" Margin="1,-5,0,0"/>



负上边距输出:

无余量输出:

更新

在情况下,你想标题是定制的,你需要有模型中的类一个属性代表行对象的)说时间这将是不同的行不同。对于第一行时间值将是9秒10等

In case you want header to be customizable, you need to have one property in your model class(object representing row) say Time which will be different for different rows. For first row Time value will be 9, second 10 and so on.

然后你可以绑定到像这样的属性:

Then you can bind to that property like this:

<StackPanel Orientation="Horizontal" VerticalAlignment="Center" 
            HorizontalAlignment="Center">
    <TextBlock Text="{Binding Path=DataContext.Time,
                        RelativeSource={RelativeSource Mode=FindAncestor,
                                                AncestorType=DataGridRowHeader}}"
                Foreground="#9493CF" FontSize="16" />
    <TextBlock Text="00" Foreground="#9493CF" />
</StackPanel>

这篇关于如何绑定行标题在WPF数据网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆