How to access a button from a DataGridTemplateColumn.CellTemplate

I have the following .xaml for a DataGrid and you want to show / hide the button on a specific condition in the .cs code .. xaml code looks like this

<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"/> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> 

and .cs is like

 foreach (sp_ProcessingJobsResult item in grdUnConfirmJobs.ItemsSource) { var row = grdUnConfirmJobs.ItemContainerGenerator.ContainerFromItem(item) as System.Windows.Controls.DataGridRow; if (item.Status == "Cancellation Requested.") { //how find control } } 
+6
source share
1 answer

For this you need to use Binding. So take a quick approach:

Here is the code for this:

 public partial class MainWindow : Window { public bool ButtonIsVisible { get { return (bool)GetValue(ButtonIsVisibleProperty); } set { SetValue(ButtonIsVisibleProperty, value); } } // Using a DependencyProperty as the backing store for ButtonIsVisible. This enables animation, styling, binding, etc... public static readonly DependencyProperty ButtonIsVisibleProperty = DependencyProperty.Register("ButtonIsVisible", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(true)); public ObservableCollection<Person> items { get; set; } public MainWindow() { InitializeComponent(); items = new ObservableCollection<Person>(); items.Add(new Person() { Name = "FirstName" }); items.Add(new Person() { Name = "SecondName" }); this.DataContext = this; } } 

This is the model for my example:

 public class Person : INotifyPropertyChanged { private string _Name; public string Name { get { return _Name; } set { _Name = value; PropertyChanged(this, new PropertyChangedEventArgs("Name")); } } public event PropertyChangedEventHandler PropertyChanged = delegate { }; } 

Your visibility is not a bool type, so we need a converter for this:

 public class BoolToVis : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var isVisible = (bool)value; return isVisible ? Visibility.Visible : Visibility.Hidden; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 

And here is the whole XAML code:

 <Window x:Class="DataGridCellsBackground.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:local="clr-namespace:DataGridCellsBackground" > <Grid> <Grid.Resources> <local:BoolToVis x:Key="BoolTovisibilityConverter"/> </Grid.Resources> <DataGrid SelectionUnit="Cell" ItemsSource="{Binding items}" AutoGenerateColumns="False"> <DataGrid.Resources> </DataGrid.Resources> <DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Name}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Content="Visible" Visibility="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.ButtonIsVisible, Converter={StaticResource BoolTovisibilityConverter}}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid> 

Indeed, you see weird binding syntax when buttons are visible. Why is this? I suggested that you do not need this functionality in the Model, so I returned to the DataContext DataGrid to achieve this DependencyProperty. I'm not quite sure about your scenario, but I have shown you some mechanisms that can be easily used.

0
source

Source: https://habr.com/ru/post/988978/


All Articles