无法从DataGrid特定单元格中获取按钮实例
本文关键字:获取 按钮 实例 单元格 DataGrid | 更新日期: 2023-09-27 17:59:10
下面是.xaml代码。我可以得到"状态"列的值,在下一步中,我必须显示/隐藏"取消"按钮。
<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")
{
}
}
据我所知,当控件在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>
这是您的代码绑定,它将用于绑定。当复选框被选中时,你会看到按钮,否则它的可见性将根据以下转换器隐藏:
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,转换器就会为你完成这项工作,将布尔值转换为Visibility枚举的项。