无法从DataGrid特定的Cell获取按钮实例

时间:2015-06-23 06:57:59

标签: c# wpf datagrid

下面是.xaml代码。我可以获得状态列的值,在下一步中我必须显示/隐藏btnCancel按钮。

    <DataGrid x:Name="grdUnConfirmJobs"  HeadersVisibility="None" IsReadOnly="True" HorizontalScrollBarVisibility="Disabled" MouseDoubleClick="grdUnConfirmJobs_MouseDoubleClick" AutoGenerateColumns="False" Background="White" >
    <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Ref}" IsReadOnly="True" Visibility="Visible" Width="60"  />
    <DataGridTextColumn Binding="{Binding Time}" IsReadOnly="True" Visibility="Visible"  Width="35" />
    <DataGridTextColumn Binding="{Binding PickUp}" IsReadOnly="True" Visibility="Visible" Width="250"  />
    <DataGridTextColumn Binding="{Binding Destination}" IsReadOnly="True" Visibility="Visible"  Width="180" />
    <DataGridTextColumn Binding="{Binding Status}" IsReadOnly="True" Visibility="Visible"  Width="40" />    
    <DataGridTemplateColumn Header="Action" Width="auto" >
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="btnConfirm" Content="Confirm" Click="ConfirmButton_Click"  Height="auto" Width="auto"  Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left" />
                    <Button x:Name="btnDecline" Content="Decline" Click="btnDecline_Click" Height="auto" Width="auto"  Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left" />
                    <Button x:Name="btnCancel" Content="Cancel" Click="btnCancel_Click" Height="auto" Width="auto"  Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left" Visibility="Visible" />
                </StackPanel>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

我可以按如下所示读取状态栏的值,但无法显示/隐藏btnCancel对象。请有人建议我该怎么做。

    using (CabeeLocalDataContext db = new CabeeLocalDataContext())
    {

    var unCnfJobs = db.sp_ProcessingJobs(CurrentDateTime).ToList();
    grdUnConfirmJobs.ItemsSource = unCnfJobs;
    }

    grdUnConfirmJobs.UpdateLayout();

    foreach (sp_ProcessingJobsResult item in grdUnConfirmJobs.ItemsSource)
    {
    if (item.Status == "Cancellation Requested")
    {

    }

    }

1 个答案:

答案 0 :(得分:0)

据我所知,在DataTemplates中定义控件时,无法按名称访问控件。要更改该按钮可见性,您必须使用绑定。我为此做了一个简单的例子:

<Window x:Class="VisibilityStack.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:converters="clr-namespace:VisibilityStack"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.Resources>
        <converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
    </Grid.Resources>
    <StackPanel>
        <Button Content="Testing Visibility" 
                Visibility="{Binding IsChecked, Converter={StaticResource BoolToVisibilityConverter}}"/>
        <CheckBox IsChecked="{Binding IsChecked}"/>
    </StackPanel>
</Grid>

这是您使用DependencyProperty IsChecked的代码隐藏,它将用于绑定。检查CheckBox时,您将看到该按钮,否则根据以下转换器隐藏Visibility:

public partial class MainWindow : Window
{
    public bool IsChecked
    {
        get { return (bool)GetValue(IsCheckedProperty); }
        set { SetValue(IsCheckedProperty, value); }
    }

    // Using a DependencyProperty as the backing store for IsChecked.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsCheckedProperty =
        DependencyProperty.Register("IsChecked", typeof(bool), typeof(MainWindow), new PropertyMetadata(false));


    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }
}

转换器:

public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        bool val = (bool)value;

        return val ?
          Visibility.Visible : Visibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

因此,在您的代码隐藏中,如果根据您的业务逻辑,您需要隐藏按钮,或者可见只需将IsChecked属性更改为true或false,转换器将为您完成工作,将布尔值转换为可见性枚举。