Capture Click Event

Nov 5, 2008 at 12:18 AM

Very needed control., thanks! But I'm struggling because I need to manupulate the grid during run-time and there is very limited info on that. For example, I'm trying to capture the click event, so the user can essentially "drill-down" into record and display a new window based on selected value. I have tried the "SingleClick Editing" sample, but it doesn't work. Can you provide more samples on how to wire-up and intercept events?

Thanks,

P.S. As my little contribution here is some sample code for creating columns run-time:

private CustomerList _customerList;

 

 

using (DataTable dtReflection = ResQ.Converters.DataTableConverter.GetDataTableNative(_customerList))

 

{

 

foreach (DataColumn c in dtReflection.Columns)

 

{

 

if (c.DataType == typeof(Boolean))

 

{

 

DataGridCheckBoxColumn dgc = new DataGridCheckBoxColumn();

 

dgc.Header = c.Caption;

dgc.Binding =

new Binding(c.ColumnName);

 

dgCustomers.Columns.Add(dgc);

}

 

else

 

 

{

 

 

DataGridTextColumn dgc = new DataGridTextColumn();

 

dgc.Header = c.Caption;

dgc.Binding =

new Binding(c.ColumnName);

 

dgCustomers.Columns.Add(dgc);

}

}

}

Coordinator
Nov 5, 2008 at 2:21 PM
Could you be a little more specific about what the problem is.  When you say the "Single Click Editing" solution doesn't work, what doesn't work about it?  The event isn't called?  Is what you're looking for just to attached to the click event on a cell?  If so, you cannot use MouseLeftButtonDown as that is captured by DataGridCell.cs.  You can use PreviewMouseLeftButtonDown or MouseDoubleClick.  You can also work around it by using a DataGridTemplateColumn and attach a Click event on a custom element that you create.
Nov 5, 2008 at 3:49 PM

Yes, the event wasn’t called. And I get why now. I will try your tips, now. Thanks, vinsibal!

Nov 5, 2008 at 10:22 PM

Just now got to it and found that I was, infact, using PreviewMouseLeftButtonDown from the "SingleClick" tip. MouseDoubleClick not getting called as well. Do I still need to use DataGridTemplateColumn with these events? Also, can you provide some samples on how to bind DataGridComboboxColumn run-time in code? Also, some samples of wiring events in the code would be appreciated as well.

here is my code:

 

<!--XAML code-->

 

<Style x:Key="SingleClickEditing" TargetType="{x:Type dg:DataGridCell}">

 

 

<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>

 

 

<EventSetter Event="MouseDoubleClick" Handler="DataGridCell_MousedoubleClick"></EventSetter>

 

 

</Style>

 

 


//Code-behind:
private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)    {}

 

 

private void DataGridCell_MouseDoubleClick(object sender, MouseButtonEventArgs e)    {}

 

 

Thanks a lot!

Nov 6, 2008 at 1:11 AM
Alright, I've got this working:

<!-- XAML-->

<dg:DataGrid.Resources>

 

 

<!--SINGLE CLICK EDITING-->

 

 

<Style x:Key="SingleClickEditing" TargetType="{x:Type dg:DataGridCell}">

 

 

<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>

 

 

</Style>

 

 

</dg:DataGrid.Resources> 
<dg:DataGridComboBoxColumn Header="Type" Width="*"

 

 

ItemsSource="{Binding Source={x:Static Data:CheckBook.Descriptions}, Mode=OneWay}"

 

 

TextBinding="{Binding CustType}">

 

 

<dg:DataGridComboBoxColumn.EditingElementStyle>

 

 

<Style TargetType="ComboBox">

 

 

<Setter Property="IsEditable" Value="True" />

 

 

</Style>

 

 

</dg:DataGridComboBoxColumn.EditingElementStyle>

 

 

</dg:DataGridComboBoxColumn>

 

So, how I can code this in code-behind? Particulary the CellStyle as it's the piece that's tying up the event?


Thanks.

 

Coordinator
Nov 6, 2008 at 2:26 AM
For the CellStyle you can do this:

Style

 

style = new Style(typeof(DataGridCell));
EventSetter eventSetter = new EventSetter
{
    Event =
DataGridCell.PreviewMouseLeftButtonDownEvent,
    Handler =
new MouseButtonEventHandler(DataGridCell_PreviewMouseLeftButtonDown)
};
style.Setters.Add(eventSetter);
DataGrid_Standard.CellStyle = style;