Datagrid中的IsEnabled按钮mvvm wpf

时间:2017-08-08 06:09:05

标签: c# wpf mvvm datagrid

如何使用mvvm在datagrid中启用/禁用按钮,其中列状态等于c#中的某个值?

感谢。

P.S。我是mvvm和wpf的新手。

编辑:

我的代码只在Datagrid中打印数据。我想在列状态等于1时禁用编辑按钮。

您可以在下面看到我的代码。

模型文件

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文件

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文件:

<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>

1 个答案:

答案 0 :(得分:1)

您可以将StyleDataTrigger

一起使用
<Button x:Name="EditButton">
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Status}" Value="1">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>