我有一個包含8列數據網格的XAML窗口。它讀取兩個平面文件並使用這些文件包含的值填充數據網格。 (我沒有包含代碼來做到這一點,因爲它工作正常,與我的問題無關。)我希望用戶能夠編輯「From」和「To」列中的值,但不能在任何其他。目前發生的情況是,當用戶點擊一個單元格時,整行會突出顯示(變爲藍色),但單擊的單元格也會以粗體邊框突出顯示。如果用戶點擊另一個單元格,則不會發生任何事情(除了突出顯示新行和單元格)。如果用戶突出顯示的細胞再次點擊,或者在鍵盤上按下一個鍵,就出現以下錯誤:如何讓用戶更改XAML數據網格中的值?
拋出異常:「System.InvalidOperationException」在PresentationFramework.dll
其他信息:「EditItem」這個視圖是不允許的。
我在做什麼錯?
這裏是XAML的定義:
<Window x:Class="CrewPlanEditor.Validate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Check Duties" Height="571" Width="753" MinWidth="674" MinHeight="431" Background="#b3d9ff" Icon="CrewPLAN.ico">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<DataGrid x:Name="DgValidate" Grid.ColumnSpan="3" FontWeight="Normal" ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Width="60" Header="Crew" Binding="{Binding DutyNo}" />
<DataGridTextColumn Width="60" Header="Run" Binding="{Binding BusNo}" />
<DataGridTextColumn Width="60" Header="From" Binding="{Binding From}" />
<DataGridTextColumn Width="60" Header="To" Binding="{Binding To}" />
<DataGridTextColumn Width="60" Header="Brk" Binding="{Binding Next}" />
<DataGridTextColumn Width="60" Header="Type" Binding="{Binding DutyTypeName}" />
<DataGridTextColumn Width="60" Header="Depot" Binding="{Binding Depot}" />
<DataGridTextColumn Width="60" Header="Cost" Binding="{Binding Cost}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
這裏是C#背後:
using System.Windows;
using CrewPlanEditor.Contexts;
using IO = System.IO;
namespace CrewPlanEditor
{
/// <summary>
/// Interaction logic for Validate.xaml
/// </summary>
public partial class Validate
{
public Validate(string jobFolder)
{
var dutiesFile = IO.Path.Combine(jobFolder, "Data", "Duties.txt");
var dutyTypesFile = IO.Path.Combine(jobFolder, "Data", "DutyTypes.txt");
var duties = new DutiesFileReader().ReadFile(null, dutiesFile, dutyTypesFile).GridDutyData;
DataContext = duties;
InitializeComponent();
}
}
}