views:

72

answers:

1

hello, i'm new in wpf. I have a DataGrid form wpftoolkit and i need to show button in template only when row is selected and the record is not last(new record item)

<dg:DataGrid AutoGenerateColumns="False" DockPanel.Dock="Top"   
                 ItemsSource="{Binding Source={StaticResource Entries}}"  
                 Name="dataGrid"  >

                <dg:DataGrid.Columns>
                    <dg:DataGridTextColumn Header="Term" Width="2*" Binding="{Binding Path=Term}"/>
                    <dg:DataGridTextColumn Header="Definition" Width="5*" Binding="{Binding Path=Definition}"/>

                    <dg:DataGridTemplateColumn Header="">
                        <dg:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Click="btnRemove_Click">Remove</Button>
                            </DataTemplate>
                        </dg:DataGridTemplateColumn.CellTemplate>
                    </dg:DataGridTemplateColumn>

                </dg:DataGrid.Columns>
            </dg:DataGrid>

How to bind the Visibility property with the datagrid?

my not-completely solution which disables the button when row is not selected:

<dg:DataGridTemplateColumn >
                        <dg:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Remove" Click="btnRemove_Click"   >
                                    <Button.Style>
                                        <Style TargetType="{x:Type Button}"  >
                                            <Setter Property="Visibility" Value="Hidden" />
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dg:DataGridRow}}, Path=IsSelected}" Value="True">
                                                    <Setter Property="Visibility" Value="Visible" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Button.Style>
                                </Button>
                            </DataTemplate>
                        </dg:DataGridTemplateColumn.CellTemplate>
                    </dg:DataGridTemplateColumn>
A: 

One solution is to write a ValueConverter for the Visibility property which is Binded to the DataRow. On the ValueConverter check to see if its a new row then set Visibility to be Hidden else Visible.

Vivek
how do i check is this row is new. I didn't found this property that indicates that this row is new. If i knew this i writed a multidatatrigger.
2xMax
You can use the DataRow.RowState property to check if the DataRowState is DataRowState.Added.
Vivek