0
votes

I have a datagrid with one column as template column ,and I added a button to It . My xaml is :

 <dg:DataGridTemplateColumn Header="Generate SlNo" Width="100">
     <dg:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <Button Name="btnGenerateSlNO" Width="100" IsEnabled="{Binding IsSerialNoProduct,Mode=TwoWay}" Click="btnGenerateSlNO_Click">Generate SlNo</Button>
        </DataTemplate>
     </dg:DataGridTemplateColumn.CellTemplate>
 </dg:DataGridTemplateColumn>

My Data Class :

 public class clsPurchaseBillEntryList : INotifyPropertyChanged, IDataErrorInfo
{

    private bool _IsSerialNoProduct;
     public bool IsSerialNoProduct
    {
        get { return _IsSerialNoProduct; }
        set
        {
            _IsSerialNoProduct = value;
            OnPropertyChanged("IsSerialNoProduct");
        }
    }
}

Now I want to enable and disable the button based on the Property IsSerialNoProduct.My problem is when the form is first load the button is showed as enabled .The property got value only when I click a cell in the datagrid because only my classes constructor is worked.I want to disable the button on form load how to accomplish this?

2

2 Answers

1
votes

I want to enable and disable the button based on the Property IsSerialNoProduct

Do not do this.
WPF uses excellent concept of commands. If you're using data binding, you can use ICommand implementations like DelegateCommand or RelayCommand, and bind button to that command:

public class clsPurchaseBillEntryList
{
    // ...
    public clsPurchaseBillEntryList()
    {
         DoSomethingCommand = new RelayCommand(DoSomething, () => IsSerialNoProduct);
    }

    private void DoSomething()
    {
    } 
    public RelayCommand DoSomethingCommand { get; private set; }
}

Then, in XAML write:

<Button Name="btnGenerateSlNO" Width="100" Command="{Binding DoSomethingCommand}">Generate SlNo</Button>
0
votes

In xaml:

<Datagrid Binding={Binding SerialNumber}>
 <dg:DataGridTemplateColumn Header="Generate SlNo" Width="100">
  <dg:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <Button Name="btnGenerateSlNO" Width="100" IsEnabled="{Binding IsSerialNoProduct,Mode=TwoWay}" Click="btnGenerateSlNO_Click">Generate SlNo</Button>
    </DataTemplate>
  </dg:DataGridTemplateColumn.CellTemplate>
 </dg:DataGridTemplateColumn>
</Datagrid>

In ViewModel:

public ObservableCollection<clsPurchaseBillEntryList > SerialNumber { get; set; }

Class clsPurchaseBillEntryList :

public class clsPurchaseBillEntryList 
{
    public bool IsSerialNoProduct { get; set; }
}

Changing value of IsSerialNoProduct will give the expected response.

Note: Untested code