单击时数据网格行按钮不会触发。 [英] data grid row button is not fire on single click.
本文介绍了单击时数据网格行按钮不会触发。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我填写详细信息并将其绑定在数据网格中时,我必须在编辑按钮上单击数据网格行中的单击事件。单击无法执行此操作,但它会触发在第二次点击。
我使用了以下代码:
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< StackPanel>
< Button Grid.Row =" 1"名称= QUOT; PreviewNotificationPopup"
Click =" PreviewNotificationPopup_Click"
VerticalAlignment =" Center"的HorizontalAlignment = QUOT;中心"的IsEnabled = QUOT;真" Style =" {StaticResource addBtn}">
< Image Source =" Resources / preview.png"宽度= QUOT; 24英寸; />
< / Button>
< / StackPanel>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
解决方案
Hi Jai,
我尝试在我身边测试你的代码,我可以通过单击点击按钮点击事件,如下所示:
< Grid>
< DataGrid
ItemsSource =" {Binding}"
Name =" datagrid1"
Width =" 400"
Height =" 300"
AutoGenerateColumns =" False">
< DataGrid.Columns>
< DataGridTextColumn
Width =" 50"
Binding =" {Binding Id}"
Header =" Id" />
< DataGridTextColumn
Width =" 80"
Binding =" {Binding Name}"
Header =" Name" />
< DataGridTextColumn
Width =" 100"
Binding =" {Binding Birthday}"
Header =" Birthday" />
< DataGridTemplateColumn Width =" 100"头= QUOT; ButtonColumn">
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< StackPanel>
< Button
Name =" PreviewNotificationPopup"
HorizontalAlignment =" Center"
VerticalAlignment =" Center"
Click =" PreviewNotificationPopup_Click"
IsEnabled =" True">
< Image Width =" 24"源= QUOT; ../照片/ 1.JPG" />
< / Button>
< / StackPanel>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
< /DataGrid.Columns>
< / DataGrid>
< / Grid>.cs:
public partial class Window1:Window
{
公开列表<用户>用户{get;组; }
public Window1()
{
InitializeComponent();
users = new List< User>();
users.Add(new User(){Id = 1,Name =" John Doe",Birthday = new DateTime(1971,7,23)});
users.Add(new User(){Id = 2,Name =" Jane Doe",Birthday = new DateTime(1974,1,17)});
users.Add(new User(){Id = 3,Name =" Sammy Doe",Birthday = new DateTime(1991,9,2)});
this.DataContext = users;
}
private void PreviewNotificationPopup_Click(object sender,RoutedEventArgs e)
{
MessageBox.Show(""点击按钮");
}
}
公共类用户
{
public int Id {get;组; }
public string Name {get;组; }
public DateTime Birthday {get;组; }
}
最好的问候,
Cherry
When I fill the details and bind those in data grid, then I have to fire the click event on edit button click in the row of data grid. Unable to do this in single click, but it fires on the second click.
I have used following code:
<DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel> <Button Grid.Row="1" Name="PreviewNotificationPopup" Click="PreviewNotificationPopup_Click" VerticalAlignment="Center" HorizontalAlignment="Center" IsEnabled="True" Style="{StaticResource addBtn}"> <Image Source="Resources/preview.png" Width="24"/> </Button> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate>
解决方案
Hi Jai,
I have try to test your code at my side, and I can fire Button click event by single click, like this:
<Grid> <DataGrid ItemsSource="{Binding}" Name="datagrid1" Width="400" Height="300" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Width="50" Binding="{Binding Id}" Header="Id" /> <DataGridTextColumn Width="80" Binding="{Binding Name}" Header="Name" /> <DataGridTextColumn Width="100" Binding="{Binding Birthday}" Header="Birthday" /> <DataGridTemplateColumn Width="100" Header="ButtonColumn"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel> <Button Name="PreviewNotificationPopup" HorizontalAlignment="Center" VerticalAlignment="Center" Click="PreviewNotificationPopup_Click" IsEnabled="True"> <Image Width="24" Source="../Photo/1.jpg" /> </Button> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid>.cs:
public partial class Window1 : Window { public List<User> users { get; set; } public Window1() { InitializeComponent(); users = new List<User>(); users.Add(new User() { Id = 1, Name = "John Doe", Birthday = new DateTime(1971, 7, 23) }); users.Add(new User() { Id = 2, Name = "Jane Doe", Birthday = new DateTime(1974, 1, 17) }); users.Add(new User() { Id = 3, Name = "Sammy Doe", Birthday = new DateTime(1991, 9, 2) }); this.DataContext = users; } private void PreviewNotificationPopup_Click(object sender, RoutedEventArgs e) { MessageBox.Show("button is clicked"); } } public class User { public int Id { get; set; } public string Name { get; set; } public DateTime Birthday { get; set; } }Best Regards,
Cherry
这篇关于单击时数据网格行按钮不会触发。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文