将Datagrid中的按钮内容绑定到列值如果condition为true

时间:2018-02-14 12:39:14

标签: c# wpf datagrid

我有DataGrid我有几列。我在Button内也有一个DataGrid。现在当DataGrid加载我想要更改按钮内容。我想检查“状态列”的状态。然后根据我要更改DataGrid内的按钮内容。这就是我想要实现的目标

E.g

if(Status="Accepted"){
closeorderbtn.content="Close Order"
}

else{
closeorderbtn.content="Open Order"
}

Xaml文件

<DataGrid x:Name="dataGrid" FlowDirection="RightToLeft" SelectionMode="Single" MouseLeftButtonUp="dataGrid_MouseLeftButtonUp" MouseDoubleClick="dataGrid_MouseDoubleClick" Background="WhiteSmoke" RowBackground="White" HorizontalContentAlignment="Center" HorizontalAlignment="Left" ColumnWidth="*" Height="534" VerticalAlignment="Top" Width="1174" IsReadOnly="True" AutoGenerateColumns="False" BorderThickness="1" BorderBrush="LightGray" GridLinesVisibility="All" Margin="4,6,0,0">
    <DataGrid.Columns>

        <DataGridTextColumn Header="Total_Quantity" Binding="{Binding Total_Quantity}" FontFamily="B Nazanin" FontWeight="Bold"/>
        <DataGridTextColumn Header="Rate" Binding="{Binding RatePerTon, StringFormat=\{0:n0\}}" FontFamily="B Nazanin" FontWeight="Bold"/>
        <DataGridTextColumn Header="Discount" Binding="{Binding Discount}" FontFamily="B Nazanin" FontWeight="Bold"/>
        <DataGridTextColumn Header="Final_" Binding="{Binding Final_Cost,StringFormat=\{0:n0\}}" FontFamily="B Nazanin" FontWeight="Bold"/>
        <DataGridTextColumn Header="Payment_" Binding="{Binding Payment_Mode}" FontFamily="B Nazanin" FontWeight="Bold"/>
        <DataGridTextColumn Header="status" Binding="{Binding Status}" FontFamily="B Nazanin" FontWeight="Bold"/>
        <DataGridTemplateColumn Width="80" >

            <DataGridTemplateColumn.CellTemplate >
                <DataTemplate>
                    <Button x:Name="closeorderbtn" Content=""   Click="CloseOrder" Background="#FF5890BF" Foreground="#FFBFBCBC"   />


                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>

</DataGrid>

CS

IQueryable<Order> result;

result = (from s in ManagerClass.oSqlData.Orders select s);

dataGrid.ItemsSource = result;

1 个答案:

答案 0 :(得分:1)

您可以将StyleDataTrigger

一起使用
<Button x:Name="closeorderbtn" Content="" Click="CloseOrder" Background="#FF5890BF" Foreground="#FFBFBCBC">
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="Content" Value="Open Order" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Status}" Value="Accepted">
                    <Setter Property="Content" Value="Close Order" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>