Hello,
I am using a XamDatagrid with a templatefield. This templatefield has a datatemplate with a Stackpanel. Inside the Stackpanel are: An icon, a textblock and a button.
I want to make the button visibily only when the cursor is over the templatefield. What I got so far: When the cursor is over the StackPanel. It fails on the button itself and also on the unused spaced between end of Stackpanel and templatefield.
Current XAML see below.
Please advice how the buttons visibility can be controlled by the cursor hoovering over the whole templatefield.
Thanks
Niko
<igDP:TemplateField.DisplayTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" Name="TeamStackPanel"> <Image Source="pack://application:,,,/Bauradar.Common;component/images/Small/Team_16x16.png" Margin="0,0,5,0"/> <TextBlock VerticalAlignment="Center" Text="{Binding .}" FontWeight="Bold" /> <Button Name="NeueArbeitszeit" Command="{Binding Path=Data.AddRessourceOnTopCommand, Source={StaticResource Proxy}}" Background="Transparent" BorderBrush="Transparent" Height="16" Width="16" VerticalAlignment="Center" Margin="5,0,0,0"> <Image Source="/Bauradar.Common;component/images/Small/Mitarbeiter_neu_16x16.png" /> <Button.Resources> <Style TargetType="Button"> <Style.Triggers> <DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=TeamStackPanel}" Value="False"> <Setter Property="Visibility" Value="Collapsed"/> </DataTrigger> </Style.Triggers> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border> <Border.Style> <Style TargetType="{x:Type Border}"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="BorderBrush" Value="Gray"/> <Setter Property="BorderThickness" Value="1"/> </Trigger> </Style.Triggers> </Style> </Border.Style> <Grid Background="Transparent"> <ContentPresenter></ContentPresenter> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Button.Resources> </Button> </StackPanel> </DataTemplate> </igDP:TemplateField.DisplayTemplate> </igDP:TemplateField>
Hi Niko,
Thank you for sharing the solution that you’ve found!
Best regards,
Bozhidara Pachilova
thanks for the example.
I had to adapt it in my app and this finally works:
I assume that the difference between this XAML and the sample above is about the different nesting inside the templatefield.
Thanks for helping out here.
<igDP:TemplateField.DisplayTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" Name="TeamStackPanel" Background="Transparent"> <Image Source="pack://application:,,,/Bauradar.Common;component/images/Small/Team_16x16.png" Margin="0,0,5,0"/> <TextBlock VerticalAlignment="Center" Text="{Binding .}" FontWeight="Bold" /> <Button Background="Transparent" BorderBrush="Transparent" Height="16" Width="16" VerticalAlignment="Center" Margin="10,0,0,0" Command="{Binding Path=Data.AddRessourceOnTopCommand, Source={StaticResource Proxy}}" CommandParameter="{Binding Path=(igEditors:TemplateEditor.Editor).DataContext.DataItem, RelativeSource={RelativeSource Self}, Mode=OneWay}"> <Image Source="/Bauradar.Common;component/images/Small/Mitarbeiter_neu_16x16.png" /> </Button> </Stackpanel> </Datatemplate> </igDP:TemplateField.DisplayTemplate>
Hello Niko,
I think that the easiest way to find which is the row, the user clicked the button on, is to use the click event handler for the button and then get its TempleteParent, which should be the CellValuePresenter, from which the Record object could be accessed. It could be done with the following code:
CellValuePresenter presenter = (sender as Button).TemplatedParent as CellValuePresenter; if (presenter != null) { var rowData = presenter.Record; // add any logic concerning the current record }
I am also attaching a small sample that demonstrates what I have explained above.
Additionally, I have found a forum post that provides a sample of how this could be done using a command binding, you can find the forum post below:
https://es.infragistics.com/community/forums/f/ultimate-ui-for-wpf/93405/button-in-each-row-in-the-last-column-in-xamdatagrid
Please let me know if you need any further assistance.
Regards, Ivan Kitanov
TemplateColumnButtonGetRecord.zip
follow up question: The button has a command binding -> works fine.
Is there a way to send the datacontext of the current templatefield as paramter with the command?
Or otherwise: How can I identify the row I pressing the button?
I am glad that you found a workaround. I have tested this as well and I confirm it works that way. If you need any further assistance on the matter, please, let us know.
Best regards, Bozhidara Pachilova Associate Software Developer