The program uses several DataGrid with the same set of columns. There are a lot of them, so you don't want to just copy the code.
Tell me, is it possible to render only columns in a style?
Something like that:
<Style x:Key="DataGridStyle" TargetType="{x:Type DataGrid}"> <Setter Property="DataGrid.Columns"> <Setter.Value> <DataGridTextColumn Header="Id" Binding="{Binding Product.Id}" /> <DataGridTextColumn Header="Название" Binding="{Binding Product.Title}" /> <DataGridTextColumn Header="Цена" Binding="{Binding Product.Price}" /> </Setter.Value> </Setter> </Style> Update
The program provides the user with 3 tables with goods: general, favorites and blacklist.
General table:
<DataGrid ItemsSource="{Binding Products}" > <DataGrid.ContextMenu> <ContextMenu> <MenuItem Header="Открыть в браузере" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}" Command="{Binding ResultsVM.OpenProductCommand}" /> <MenuItem Header="Добавить в избранное" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}" Command="{Binding ResultsVM.PutToFavoritesCommand, IsAsync=True}" /> <MenuItem Header="Убрать из избранного" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}" Command="{Binding ResultsVM.RemoveFromFavoritesCommand}" /> <MenuItem Header="Добавить в черный список" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}" Command="{Binding ResultsVM.PutToBlackListCommand}" /> <MenuItem Header="Убрать из черного списка" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}" Command="{Binding ResultsVM.RemoveFromBlacklistCommand}" /> <MenuItem Header="Удалить" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}" Command="{Binding ResultsVM.DeleteCommand}" /> </ContextMenu> </DataGrid.ContextMenu> <DataGrid.Columns> <DataGridTextColumn Header="Id" Binding="{Binding Product.Id}" /> <DataGridTextColumn Header="Название" Binding="{Binding Product.Title}" /> <DataGridTextColumn Header="Цена" Binding="{Binding Product.Price}" /> </DataGrid.Columns> </DataGrid> Favorites:
<DataGrid ItemsSource="{Binding Favorites}"> <DataGrid.ContextMenu> <ContextMenu> <MenuItem Header="Открыть в браузере" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}" Command="{Binding ResultsVM.OpenProductCommand}" /> <MenuItem Header="Убрать из избранного" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}" Command="{Binding ResultsVM.RemoveFromFavoritesCommand}" /> <MenuItem Header="Удалить" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}" Command="{Binding ResultsVM.DeleteCommand}" /> </ContextMenu> </DataGrid.ContextMenu> <DataGrid.Columns> <DataGridTextColumn Header="Id" Binding="{Binding Product.Id}" /> <DataGridTextColumn Header="Название" Binding="{Binding Product.Title}" /> <DataGridTextColumn Header="Цена" Binding="{Binding Product.Price}" /> </DataGrid.Columns> </DataGrid>
UserControl? What will be different in the tables? - VladDUserControl'a. I will write tomorrow, today is too late. - VladD