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>
–
–
<Style.Triggers>
<DataTrigger Binding="{Binding Status}" Value="1">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
–
–
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.