如何在WPF中的Datagrid内部进行控制?

时间:2015-06-02 14:14:57

标签: wpf

我陷入了一个小问题,根据我的项目关注而变得耗时,我在我的wpf项目中使用datagrid,其中我的要求是仅在状态为“未付费”时才显示付款按钮,这里我使用了DataGridTemplateColumn,我使用Button和网格只能获得付款状态。 这是我的xaml文件

  <DataGrid Name="gridInvoiceList" SelectedItem="{Binding SelectedDetails}"  ColumnHeaderHeight="35"  ItemsSource="{Binding ItemsSource, ElementName=pageControl, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"   CanUserAddRows="false" 
        HorizontalAlignment="Left"     AutoGenerateColumns="False" 
       Width="978" RowHeaderWidth="20" RowHeight="25" 
           CanUserSortColumns="False" SelectionChanged="gridInvoiceList_SelectionChanged" Height="220" LoadingRow="gridInvoiceList_LoadingRow" SelectionMode="Extended" >
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Name" Width="160" Binding="{Binding FullName }" CanUserSort="True"   IsReadOnly="True"  />
                            <DataGridTextColumn Header="Total Amount" Width="150" Binding="{Binding TotalAmount,ConverterCulture='en-GB',StringFormat=-{0:C}}" CanUserSort="True"  IsReadOnly="True"  />
                            <DataGridTextColumn Header="Invoice Date" Width="200" Binding="{Binding InvoiceDate}" CanUserSort="True"  IsReadOnly="True"/>
                            <DataGridTextColumn Header="Payment Status" Width="220" Binding="{Binding PaymentStatus}" CanUserSort="True"  IsReadOnly="True"  />
                            <DataGridTemplateColumn Header="Pay" Width="105">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button Content="Pay" HorizontalAlignment="Center"  VerticalAlignment="Top"    Name="btnPay" Click="btnPay_Click"  />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn Header="Print" Width="105">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button Content="Print" HorizontalAlignment="Center"  VerticalAlignment="Top"   Name="btnPrintInvoice"   Click="btnPrint_Click"   />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>

我使用了参考代码中的一些代码,这些代码没有满足我的要求

1 个答案:

答案 0 :(得分:2)

您可以使用converter来实现这一目标。但我会告诉你最简单,最简单的方法:

创建一个属性:

public System.Windows.Visibility PayButtonVisibility
{
    get
    {
        if (PaymentStatus == MyEnum.NotPaid)
            return System.Windows.Visibility.Visible;

        return System.Windows.Visibility.Collapsed;
    }
}

然后将按钮visibility绑定到它:

<Button Content="Pay" HorizontalAlignment="Center"  VerticalAlignment="Top"    Name="btnPay" Click="btnPay_Click" Visibility="{Binding PayButtonVisibility}" />