在我的程序中,我有一个 UserControl,其中包含一个DataGrid
绑定ItemsSource
到ObservableCollection
. 为此,DataGrid
我需要能够禁用特定单元格并将其变灰。我想最好在 c++ 中执行此操作,因为我可能需要更改在运行时发生的单元格。我知道如何使用IsReadOnly
,但似乎我只能为整个列切换它。这成为一个问题,因为我的列绑定到数据,这使我更难定位特定的网格单元格。
话虽如此,
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>
数据模型:
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
它是 View 控件的特征,而不是ObservableCollection
.