1

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.

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,........

My Code-

<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 -

enter image description here

What I Got -

enter image description here

1

1 Answer 1

4

You can specify negative top margin on second textBlock:

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

Output with negative top margin:

enter image description here

Output without margin:

enter image description here

Update:

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>
Sign up to request clarification or add additional context in comments.

7 Comments

sir i want to set the value, 9.00, 9.30, 10.00, 10.30.... like this, but i got 1 00, 1 00,1 00, 1 00,1 00, 1 00,1 00, 1 00.. that is my question. How can i set the values in row header
I got 00,00,00 only. explain property also sir
What's ItemSource property for your DataGrid?
my project skydrive.live.com/… download my project, and see the source code sir.
Ok great. You should read more about Binding and MVVM. That will help you understand the concept for sure.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.