簡體   English   中英

WPF Datagrid-更改單擊按鈕的行的背景色

[英]WPF Datagrid-Change background color of the row for which the button is clicked

我的wpf應用程序中有一個數據網格。 我希望當用戶單擊數據網格中的任何按鈕時,對應的行應為紅色。 我認為我們可以通過使用事件觸發器來做到這一點,但我真的不知道如何使用它。

    <DataGrid x:Name="dgEmp" Grid.Row="1" AutoGenerateColumns="False" CanUserAddRows="False" >          
        <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Width="*" Binding="{Binding Path=Name}"></DataGridTextColumn>
        <DataGridTextColumn Header="Age" Width="*" Binding="{Binding Path=Age}"></DataGridTextColumn>
            <DataGridTemplateColumn Header="Delete" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Delete selected row"></Button>                              
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

您可以在每行中設置一個color屬性,將此屬性綁定到行的背景色,並在SelectedItem更改時對其進行更改。

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
       <Setter Property="Background" Value="{Binding RowColour}" />
    </Style>
</DataGrid.RowStyle>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM