none
disable button in datagrid row based on row content

    Question

  • I have a datagrid and in each row I have a button that says "View". Based on a cell content in each row I want to disable or enable the button.


    How would I accomplish this?

    Friday, October 29, 2010 12:46 PM

Answers

  • Hi,

    In this example there is a List of "Person" objects binded to a grid.
    Based on the Age the button is Disabled or Enabled.


    xaml code

    <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="SilverlightTest.GridButtonBinding"
        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"
        mc:Ignorable="d"
        xmlns:my="clr-namespace:SilverlightTest"
        d:DesignHeight="300" d:DesignWidth="400">
       
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.Resources>
                <my:EnableDisableConverter x:Key="enableDisableConverter" />
            </Grid.Resources>
                <sdk:DataGrid x:Name="dataGrid" AutoGenerateColumns="False">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                    <sdk:DataGridTextColumn Header="Age" Binding="{Binding Age}" />
                    <sdk:DataGridTemplateColumn Header="">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Click" IsEnabled="{Binding Converter={StaticResource enableDisableConverter}}"/>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                </sdk:DataGrid.Columns>
            </sdk:DataGrid>
        </Grid>
    </UserControl>


    c# code

    using System;
    using System.Collections.Generic;
    using System.Windows.Controls;
    using System.Windows.Data;

    namespace SilverlightTest
    {
        public partial class GridButtonBinding : UserControl
        {
            public GridButtonBinding()
            {
                InitializeComponent();

                List<Person> Persons = new List<Person>();
                Persons.Add(new Person { Name="John", Age = 20 });
                Persons.Add(new Person { Name = "Jack", Age = 10 });
                Persons.Add(new Person { Name = "Michel", Age = 15 });
                Persons.Add(new Person { Name = "Robert", Age = 30 });
                Persons.Add(new Person { Name = "Bill", Age = 40 });

                dataGrid.ItemsSource = Persons;
            }
        }

        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }

        public class EnableDisableConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                Person p = value as Person;
                if (p.Age >= 20)
                    return true;
                else
                    return false;
            }

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

     

    Hope this helps.

    Friday, October 29, 2010 3:12 PM

All replies

  • Hi

    The best way is to bind the "IsEnabled" property of the button to the object through a Converter.

    IsEnabled={Binding,Converter={StaticResource customConverter}}

     

    Hope this helps

    Friday, October 29, 2010 1:26 PM
  • never done custom converters, how would I pass the value from my datagrid to one?

    Friday, October 29, 2010 1:28 PM
  • Hi,

    In this example there is a List of "Person" objects binded to a grid.
    Based on the Age the button is Disabled or Enabled.


    xaml code

    <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="SilverlightTest.GridButtonBinding"
        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"
        mc:Ignorable="d"
        xmlns:my="clr-namespace:SilverlightTest"
        d:DesignHeight="300" d:DesignWidth="400">
       
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.Resources>
                <my:EnableDisableConverter x:Key="enableDisableConverter" />
            </Grid.Resources>
                <sdk:DataGrid x:Name="dataGrid" AutoGenerateColumns="False">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                    <sdk:DataGridTextColumn Header="Age" Binding="{Binding Age}" />
                    <sdk:DataGridTemplateColumn Header="">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Click" IsEnabled="{Binding Converter={StaticResource enableDisableConverter}}"/>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                </sdk:DataGrid.Columns>
            </sdk:DataGrid>
        </Grid>
    </UserControl>


    c# code

    using System;
    using System.Collections.Generic;
    using System.Windows.Controls;
    using System.Windows.Data;

    namespace SilverlightTest
    {
        public partial class GridButtonBinding : UserControl
        {
            public GridButtonBinding()
            {
                InitializeComponent();

                List<Person> Persons = new List<Person>();
                Persons.Add(new Person { Name="John", Age = 20 });
                Persons.Add(new Person { Name = "Jack", Age = 10 });
                Persons.Add(new Person { Name = "Michel", Age = 15 });
                Persons.Add(new Person { Name = "Robert", Age = 30 });
                Persons.Add(new Person { Name = "Bill", Age = 40 });

                dataGrid.ItemsSource = Persons;
            }
        }

        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }

        public class EnableDisableConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                Person p = value as Person;
                if (p.Age >= 20)
                    return true;
                else
                    return false;
            }

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

     

    Hope this helps.

    Friday, October 29, 2010 3:12 PM