开发者

WPF bind to datagrid from template

开发者 https://www.devze.com 2023-01-19 12:38 出处:网络
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)

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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消