C# 如何在绑定到ObservableCollection时禁用DataGrid中的单元格
在我的程序中,我有一个UserControl,它包含一个C# 如何在绑定到ObservableCollection时禁用DataGrid中的单元格,c#,wpf,xaml,mvvm,datagrid,C#,Wpf,Xaml,Mvvm,Datagrid,在我的程序中,我有一个UserControl,它包含一个DataGrid,它的ItemsSource绑定到一个observeCollection。对于这个DataGrid我需要能够禁用和灰显特定的单元格。我希望在C++中这样做,因为我可能需要改变在运行时发生的那些单元格。我知道如何使用IsReadOnly,但似乎我只能对整个列进行切换。这成为一个问题,因为我的列绑定到数据,这使得我更难针对特定的网格单元 话虽如此, XAML: 数据模型: namespace Program.Data_Mod
DataGrid
,它的ItemsSource
绑定到一个observeCollection
。对于这个DataGrid
我需要能够禁用和灰显特定的单元格。我希望在C++中这样做,因为我可能需要改变在运行时发生的那些单元格。我知道如何使用IsReadOnly
,但似乎我只能对整个列进行切换。这成为一个问题,因为我的列绑定到数据,这使得我更难针对特定的网格单元
话虽如此,
XAML:
数据模型:
namespace Program.Data_Models
{
public class CartIO_Model : PropertyChangedBase
{
private string test1 = "One";
private string test2 = "Two";
private string test3 = "Three";
private string DISABLEDtest4 = "Four";
private string DISABLEDtest5 = "Five";
private ObservableCollection<collectionData> _collection;
public CartIO_Model()
{
Collection = new ObservableCollection<collectionData>();
Collection.Add(new collectionData() { rowNum = 0, input = test1, output = ""});
Collection.Add(new collectionData() { rowNum = 1, input = test2, output = ""});
Collection.Add(new collectionData() { rowNum = 2, input = "", output = test3 });
Collection.Add(new collectionData() { rowNum = 3, input = "", output = DISABLEDtest4 });
Collection.Add(new collectionData() { rowNum = 4, input = DISABLEDtest5, output = ""});
}
public ObservableCollection<collectionData> Collection
{...}
}
public class collectionData
{
public int rowNum { set; get; }
public string input { set; get; }
public string output { set; get; }
}
}
namespace Program.Data\u模型
{
公共类CartIO_模型:PropertyChangedBase
{
私有字符串test1=“One”;
私有字符串test2=“两”;
私有字符串test3=“三”;
私有字符串DISABLEDtest4=“四”;
私有字符串DISABLEDtest5=“五”;
私人可观测采集(u采集);;
公共CartIO_模型()
{
集合=新的ObservableCollection();
Add(new collectionData(){rowNum=0,input=test1,output=”“});
Add(new collectionData(){rowNum=1,input=test2,output=”“});
Add(new collectionData(){rowNum=2,input=”“,output=test3});
Add(new collectionData(){rowNum=3,input=”“,output=DISABLEDtest4});
Add(new collectionData(){rowNum=4,input=DISABLEDtest5,output=”“});
}
公开收集
{...}
}
公共类收集数据
{
public int rowNum{set;get;}
公共字符串输入{set;get;}
公共字符串输出{set;get;}
}
}
在何处以及如何控制启用/禁用哪些单元格?这让我很困惑,因为
IsEnabled
是视图控件的一个特性,而不是ObservableCollection的特性。您可以为列定义CellStyle
,并根据model或viewmodel中的任何数据启用/禁用单元格。在这里,我禁用了输入列中的所有单元格,该列的输入值为Test4
<DataGrid ItemsSource="{Binding Model.Collection}" >
<DataGrid.Columns>
<!-- Row Number -->
<DataGridTextColumn Width="SizeToCells" Binding="{Binding rowNum}">
</DataGridTextColumn>
<!-- Inputs -->
<DataGridTextColumn Width="SizeToCells" Header="Inputs" Binding="{Binding input}" >
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding input}" Value="Four">
<Setter Property="IsEnabled" Value="false"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<!-- Outputs -->
<DataGridTextColumn Width="SizeToCells" IsReadOnly="False" Header="Outputs" Binding="{Binding output}" />
</DataGrid.Columns>
</DataGrid>
您可以为列定义CellStyle
,并根据model或viewmodel中的任何数据启用/禁用单元格。在这里,我禁用了输入列中的所有单元格,该列的输入值为Test4
<DataGrid ItemsSource="{Binding Model.Collection}" >
<DataGrid.Columns>
<!-- Row Number -->
<DataGridTextColumn Width="SizeToCells" Binding="{Binding rowNum}">
</DataGridTextColumn>
<!-- Inputs -->
<DataGridTextColumn Width="SizeToCells" Header="Inputs" Binding="{Binding input}" >
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding input}" Value="Four">
<Setter Property="IsEnabled" Value="false"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<!-- Outputs -->
<DataGridTextColumn Width="SizeToCells" IsReadOnly="False" Header="Outputs" Binding="{Binding output}" />
</DataGrid.Columns>
</DataGrid>
只需创建一个文本框样式
<Style x:Key="PoBodyTextBoxEditableStyle" TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Status, Converter={StaticResource PoStatusToEditableConverter}}"
Value="False">
<Setter Property="IsEnabled"
Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
并设置EditingElementStyle
<DataGridTextColumn x:Name="MyTextColumn"
Binding="{Binding MyText}"
Header="MyHeader"
Width="Auto"
EditingElementStyle="{StaticResource MyTextBoxEditableStyle}" />
只需创建一个文本框样式
<Style x:Key="PoBodyTextBoxEditableStyle" TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Status, Converter={StaticResource PoStatusToEditableConverter}}"
Value="False">
<Setter Property="IsEnabled"
Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
并设置EditingElementStyle
<DataGridTextColumn x:Name="MyTextColumn"
Binding="{Binding MyText}"
Header="MyHeader"
Width="Auto"
EditingElementStyle="{StaticResource MyTextBoxEditableStyle}" />
您希望在什么条件下禁用单元格..是否在collectionData类中某个属性的值中?它实际上取决于在不同的ViewModel->Model中选择了哪个单选按钮。这就是我将在数据网格上执行这些操作的地方吗?抱歉,我不明白,所以在选择单选按钮时,您希望禁用数据网格中的某些单元格。。但是你怎么决定那是哪个细胞。。禁用哪个单元格不取决于单元格中的数据吗?第一部分是。关于决定哪些细胞将是。。在编程上,这就是我遇到的问题。要禁用的单元格取决于其位置。例如:如果选择RadioButton1
,第12行中的第三个单元格可能会被禁用。我希望这更清楚一点…我建议您保持数据驱动,在模型上更新/创建属性,这将决定是否启用单元格。。您可以参考我的回答,了解您希望禁用单元格的条件。collectionData类中某个属性的值是否为..这实际上取决于在不同的ViewModel->Model中选择了哪个单选按钮。这就是我将在数据网格上执行这些操作的地方吗?抱歉,我不明白,所以在选择单选按钮时,您希望禁用数据网格中的某些单元格。。但是你怎么决定那是哪个细胞。。禁用哪个单元格不取决于单元格中的数据吗?第一部分是。关于决定哪些细胞将是。。在编程上,这就是我遇到的问题。要禁用的单元格取决于其位置。例如:如果选择RadioButton1
,第12行中的第三个单元格可能会被禁用。我希望这更清楚一点…我建议您保持数据驱动,在模型上更新/创建属性,这将决定是否启用单元格。。您可以参考我的回答您可以只对整个列执行此操作,或者我可以针对特定的单元格(由于单元格是在运行时确定的,我将如何确定)?这将仅禁用列中具有特定值的单元格。。当然,如果您绑定到ViewModel上的某个属性,它也可以禁用整个列。使用此策略,我将如何控制IsEnabled