简体   繁体   English

选择datagrid项时如何获取按钮并更改其颜色?

[英]How to get the button and change its color when datagrid item selected?

I have a datagrid with button included and binded to image as shown. 我有一个包含按钮的datagrid,并绑定到图像,如图所示。 How to get the button control and change its forecolor when the datagrid item is selected. 选择datagrid项时如何获取按钮控件并更改其前景色。

 <DataGrid Name="dgItems" AutoGenerateColumns="False" Grid.Row="1" Width="300" CanUserAddRows="False" SelectionChanged="dgItems_SelectionChanged">      
                    <DataGrid.Columns>
                        <DataGridTemplateColumn Width="SizeToCells" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Name="btnImg" Click="btnImg_Click" Height="160" Width="270">
                                        <Image  Source="{Binding Path=ImgUrl}" />
                                    </Button>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>

As an example you could do something like this.. 例如,您可以做这样的事情。

row.Cells[2].ButtonName.Style.BackColor = System.Drawing.Color.Red; row.Cells [2] .ButtonName.Style.BackColor = System.Drawing.Color.Red;

This is pseudo-code but you would have to know the actual cell based on the index this should be enough to get you started could you also post the code behind of the event you are trying to code the button change in..? 这是伪代码,但是您将必须基于索引知道实际的单元格,这应该足以使您入门。如果您还要在尝试对按钮更改进行编码的事件后发布代码,则还可以。 thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在WPF数据网格中选择项目时显示按钮? - How to show a button when item is selected in WPF datagrid? 选择并聚焦时如何更改数据网格单元格背景颜色? - How is it possible to change datagrid cell background color when is selected and focused? 在datagrid中选择时如何更改堆栈面板的背景颜色 - How to change background color of stack panel when selected in datagrid 在 DataGrid 中获取选定的行并更改背景颜色 - Get selected Row in DataGrid and Change the background color 如何在DataGrid Mahapp中更改选定的行颜色 - How to change selected row color in DataGrid Mahapp 如何更改DataGrid选择的行颜色WPF - How to change DataGrid selected row color WPF 在数据网格中,当行缩短时如何滚动到选定项目并且我修改了使其更改 position 的项目的信息? - In a datagrid, how to scroll to selected item when rows are shorted and I modify information of an item that makes it change position? 选择项目时更改ListViewItem内容的颜色 - Change the color of ListViewItem Content when Item is selected 使用上下文菜单时如何更改数据网格 WPF C# 中选定行的字体颜色 - How to change font color of selected row in datagrid WPF C# when using context menu 如何更改列表框中所选项目的颜色? - How to change color of selected item in ListBox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM