gpt4 book ai didi

c# - Datagrid MVMVM WPF中的IsEnabled按钮

转载 作者:行者123 更新时间:2023-12-03 11:03:07 24 4
gpt4 key购买 nike

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

谢谢。

附言我是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>

最佳答案

您可以将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>

关于c# - Datagrid MVMVM WPF中的IsEnabled按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45560480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com