1

I have 3 datagrids: grid0, gird1, grid2 binded to observable collection Watches0, Watches1, Watches2 respectively. Here is just small example, in my real project DataGrids had many columns with styles, converters etc. All datagrids are completely same except x:Name, Grid.Column and ItemSource properties. The problem is if I want to change some property or column in xaml I have to do it for all 3 datagrids. I was thinking about using styles or templates but see some problems with it. I'm interested is there a way to "copy" element in xaml?

    <DataGrid x:Name="grid0"
              Grid.Column="0"
              AllowDrop="True"
              AutoGenerateColumns="False"
              SelectionMode="Extended"
              ItemsSource="{Binding Watches0}"
              Drop="watchesGrid_Drop"
              PreviewMouseLeftButtonDown="watchesGrid_PreviewMouseLeftButtonDown">

        <DataGrid.Columns>
            <DataGridTextColumn Header="Name"
                                Binding="{Binding Name}"
                                Width="40" />
            <DataGridTextColumn Header="Value"
                                IsReadOnly="True"
                                Binding="{Binding Value}"
                                Width="60" />
        </DataGrid.Columns>

    </DataGrid>
Dork
  • 1,816
  • 9
  • 29
  • 57
  • 1
    I doubt that there is a way to do so. You will probably have to adapt some style for most of the properties and [events](http://stackoverflow.com/questions/4460201/how-to-set-an-event-function-via-a-style), and then apply one of the solutions from [this SO thread](http://stackoverflow.com/questions/5716123/wpf-datagrid-columns-in-style-or-resource) to deal with columns. Well, there is a more or less working way to [clone datagrid programmatically](http://stackoverflow.com/questions/32541/how-can-you-clone-a-wpf-object), but that is probably not what you want. – Eugene Podskal May 13 '15 at 16:58
  • What is wrong with using the style property exactly? – Felix Castor May 13 '15 at 17:22
  • @FelixCastor for start can't add to styles. – Dork May 13 '15 at 17:24
  • Are these properties included in the ``? If that is the case why not just bind them to a variable in the same way you do name etc. – Felix Castor May 13 '15 at 17:30
  • Are DataTemplate's not an option so you can just change your stuff in one resource and reference it for all 3? – Chris W. May 13 '15 at 19:02

0 Answers0