如何在DataGrid中禁用的细胞,同时结合一个ObservableCollection [英] How to disable a cell in a DataGrid while binding to an ObservableCollection
问题描述
在我的节目,我有一个包含用户控件一个的DataGrid
有它的的ItemsSource
绑定到一个的ObservableCollection
。对于的DataGrid
我需要能够禁用和变灰特定细胞。我想在C ++中最好做到这一点,因为我可能需要改变这种情况在运行时的细胞。我知道如何使用 IsReadOnly
,但现在看来,我只能切换,对于整列。这将成为因为我的列被绑定到数据,这使得它很难对我来说,针对特定的网格单元有问题。
In my program I have a UserControl that contains a DataGrid
that has it's ItemsSource
bound to an ObservableCollection
. For that DataGrid
I need to be able to disable and gray out specific cells. I would like to do this preferably in c++, because I might need to change which cells this happens to at run time. I know how to use IsReadOnly
, but it appears that I can only toggle that for an entire column. This becomes a problem because my columns are bound to data, which makes it harder for me to target specific grid cells.
有了这样说,
XAML:
<DataGrid ItemsSource="{Binding Model.Collection}" ... >
<DataGrid.Columns>
<!-- Row Number -->
<DataGridTextColumn Width="SizeToCells" IsReadOnly="True" Binding="{Binding rowNum}" />
<!-- Inputs -->
<DataGridTextColumn Width="SizeToCells" IsReadOnly="False" Header="Inputs" Binding="{Binding input}" />
<!-- Outputs -->
<DataGridTextColumn Width="SizeToCells" IsReadOnly="False" Header="Outputs" Binding="{Binding output}" />
</DataGrid.Columns>
</DataGrid>
数据模型:
Data Model:
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; }
}
}
在哪里,我怎么会控制哪些细胞被激活/禁用?这混淆了我,因为 IsEnabled
是一个视图控件的一个特点,而不是一个的ObservableCollection
。
Where and how would I control which cells are enabled/disabled? This confuses me because IsEnabled
is a trait of a View control, not that of an ObservableCollection
.
推荐答案
您可以定义 CellStyle
为您列以及启用/禁用取决于任何数据的单元格在模型或视图模型。在这里,我已禁用在有输入值TEST4
You can define CellStyle
for your Column and enable/disable the cells depending on any data in model or viewmodel. Here i have disabled all the cells in Inputs column which has input value as 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>
这篇关于如何在DataGrid中禁用的细胞,同时结合一个ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!