Is There A Way To Style A WPF DataGrid NewItem Placeholder

牧云@^-^@ 提交于 2019-11-30 04:42:48

问题


I have a simple data control defined as follows

<DataGrid x:Name="ScheduleDataGrid" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" Margin="5" ItemsSource="{Binding ScheduleItems}">
    <DataGrid.Columns>

        <DataGridTextColumn...></DataGridTextColumn>
        ....
        <DataGridTextColumn...></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

I have specified CanUserAddRows="True". As advertised, this puts a blank row at the bottom of the grid for the user to add a new row.

Is there a way to style that new item placeholder independently of the rest of the grid?


回答1:


Yes it is possible to change that placeholder row.

Take a look at this example I just made.

<Window.Resources>
    <Style TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding}" Value="{x:Static CollectionView.NewItemPlaceholder}">
                <Setter Property="Background" Value="Yellow"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <DataGrid AutoGenerateColumns="False" CanUserAddRows="True" ItemsSource="{Binding List}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Test" Binding="{Binding Path=Name, Mode=TwoWay}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

That placeholder row will appear with yellow background.



来源:https://stackoverflow.com/questions/20644998/is-there-a-way-to-style-a-wpf-datagrid-newitem-placeholder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!