I need to disable buttons in DataGrid cells based o a boolean value. When i try the code below, the entire row gets disabled.
What i've tried
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding Enabled}" Value="false">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
Button in row
<DataGridTemplateColumn Header="Azione" Width="3*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="btnEffettuaOperazioni_Click">
<TextBlock Text="Effettua operazioni"/>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Here is a working exemple. Take a look : If the checkbox is checked, only the button is disabled
MainWindow.xaml
<Window x:Class="TestWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestWPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<ScrollViewer>
<DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" ItemsSource="{Binding Items}">
<DataGrid.Resources>
<Style TargetType="{x:Type Button}" x:Key="ButtonStyle">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked}" Value="True">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding IsChecked}" />
<DataGridTextColumn Binding="{Binding Value}" />
<DataGridTemplateColumn Header="Azione" Width="3*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<Button Click="btnEffettuaOperazioni_Click" Style="{StaticResource ButtonStyle}">
<TextBlock Text="Effettua operazioni"/>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
MainWindow.xaml.cs
using System.Collections.ObjectModel;
using System.Windows;
namespace TestWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public class Data
{
public bool IsChecked { get; set; }
public string Value { get; set; }
}
public ObservableCollection<Data> Items { get; } = new ObservableCollection<Data>();
private void Populate()
{
Items.Add(new Data() { IsChecked = true, Value = "Lorem Ipsum" });
Items.Add(new Data() { IsChecked = true, Value = "Lorem Ipsum" });
Items.Add(new Data() { IsChecked = true, Value = "Lorem Ipsum" });
Items.Add(new Data() { IsChecked = true, Value = "Lorem Ipsum" });
Items.Add(new Data() { IsChecked = true, Value = "Lorem Ipsum" });
Items.Add(new Data() { IsChecked = true, Value = "Lorem Ipsum" });
Items.Add(new Data() { IsChecked = true, Value = "Lorem Ipsum" });
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Populate();
}
private void btnEffettuaOperazioni_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello World");
}
}
}