Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How to enable/disable button inside datagrid using mvvm, where column status equals some value in c#?

Thanks.

P.S. I'm new in mvvm and wpf.

Edited:

My code only prints data in Datagrid. I want to disable edit button when column status equals to 1.

You can see my code below.

Model file

public class Reestr
    private int id;
    private string date;
    private string market;
    private int status;
    public int Id
        get { return id; }
        set {
            id = value;
    public string Date
        get { return date; }
        set {
            date = value;
    public string Market
        get { return market; }
        set {
            market = value;
    public int Status
        get { return status; }
        set {
            status = value;

ViewModel file

public ReestrViewModel()
    reestr = GetListOfReestrs();
    reestr.Add(new Reestr() { Date = "01.08.2017", Market = "Market1", Status = 0 });
    reestr.Add(new Reestr() { Date = "02.08.2017", Market = "Market2", Status = 1 });
public ObservableCollection<Reestr> ReestrItems
    get { return reestr; }
public ObservableCollection<Reestr> GetListOfReestrs()
    return reestr;

Xaml file:

<DataGrid GridLinesVisibility="All"
          AutoGenerateColumns="False"
          CanUserAddRows="False"
          x:Name="ReestrDataGrid"
          ItemsSource="{Binding ReestrViewModel.ReestrItems}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ID"
                            Binding="{Binding Id}"
                            IsReadOnly="True"
                            Visibility="Hidden"
                            Width="*"/>
        <DataGridTextColumn Header="Date"
                            Binding="{Binding Date}"
                            IsReadOnly="True"
                            Width="*"/>
        <DataGridTextColumn Header="Market"
                            Binding="{Binding Market}"
                            IsReadOnly="True"
                            Width="*"/>
        <DataGridTextColumn Header="Status"
                            Binding="{Binding Status}"
                            IsReadOnly="True" 
                            Width="*"/>
        <DataGridTemplateColumn Header="Operations" Width="200">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Button x:Name="ViewButton"
                                Click="ViewButton_Click"></Button>
                        <Button x:Name="EditButton"></Button>
                    </StackPanel>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>               
</DataGrid>
                How about you show us some of your xaml and code? What do you mean by column status, how should the viewmodel know about any columns?
– grek40
                Aug 8, 2017 at 6:35
                Theoretically you should have a Command on your ViewModel that would be bound to your Button, said command needs to implement ICommand interface which also consists of CanExecute method. That CanExecute method will then be used to determine whether Button should be enabled or not.
– XAMlMAX
                Aug 8, 2017 at 7:08
            <Style.Triggers>
                <DataTrigger Binding="{Binding Status}" Value="1">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>
                By the way, how can I set my custom style in this button? Cause I can't do something like this <Button x:Name="EditButton" Style="MyCustomStyle">...</Button>
– Abduhafiz
                Aug 8, 2017 at 9:33
                <Style TargetType="Button" BasedOn="{StaticResource MyCustomStyle}">. But please ask another question if you have another issue.
– mm8
                Aug 8, 2017 at 9:33
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.