Click here to Skip to main content
15,910,277 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi people.
I have a two DataGrids.
Both have a column with Shape of Rectangle, I mean all cells have Rectangle with same attributes.
How may I bind the Fill and the Height of Rectangles in DataGrid1 to Rectangles of DataGrid2 in WPF ?
Thanks

What I have tried:

XML
<Grid>
    <DataGrid Name="DataGrid01">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Shape1">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Rectangle x:Name="Rectangle01"
                                   Fill="Blue"
                                   Width="20"
                                   Height="20"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    <DataGrid Name="DataGrid02">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Shape2">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Rectangle x:Name="Rectangle02"
                                   Fill="{Binding Path=Fill, ElementName=Rectangle01}"
                                   Width="20"
                                   Height="{Binding Path=RenderSize.Height, ElementName=Rectangle01}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
Posted
Updated 20-May-24 0:56am
v3

1 solution

Binding DG1 to DG2 makes no sense.

Use ResourceDictionary
ResourceDictionary and XAML resource references - Windows apps | Microsoft Learn[^]

or create a DataGrid Usercontrol as template for your DataGrids.
 
Share this answer
 
Comments
Sh.H. 20-May-24 13:26pm    
@Jo_vb.net
Thanks for comment.
I don't want to binding DG1 to DG2.
I want to bind Rectangle01 to Rectangle02.

And I should add that, the reason I want to do is that, the Fill and the Height of Rectangle01, is always different by user, and not static. So I cannot use resource references or static resource. That is why I want the Rectangle02 goes follow Rectangle01.
Jo_vb.net 21-May-24 5:02am    
I think only binding the row height (which is your other question) could work.
https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/how-to-share-sizing-properties-between-grids?view=netframeworkdesktop-4.8
Sh.H. 21-May-24 5:47am    
@Jo_vb.net
Unfortunately I tried that.
But you know, default value of RowHeight in DataGrid is NaN. And I don't know why after rendering in monitor, it is still NaN. Therefore when I bind to it, nothing happens.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900