WPF从模板绑定到datagrid

时间:2010-10-12 14:28:58

标签: c# wpf binding

我是wpf的新手。我有一个DataGrid表格wpftoolkit,我只需要在选择行并且记录不是最后一个(新记录项目)时在模板中显示按钮

<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>

如何将Visibility属性与datagrid绑定?

我没有完全解决方案,在未选择行时禁用按钮:

<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>

1 个答案:

答案 0 :(得分:0)

一种解决方案是为Visibility属性编写ValueConverter,该属性被绑定到DataRow。在ValueConverter上检查它是否是一个新行,然后将Visibility设置为Hidden else Visible。