在WPF表单上,我有一个DataGrid
和几个TextBox
元素,它们使用双向绑定绑定到单个对象中包含的不同属性。所有数据都显示得很好,TextBox
元素中的数据将允许用户编辑和更新而不会出现问题。当我在DataGrid
中编辑一个值时,我会得到一个"双向绑定需要Path或XPath"离开编辑过的单元格时出现异常知道为什么吗?
数据对象定义为:
public class Options : INotifyPropertyChanged
{
private ObservableCollection<string> ignoreList;
private decimal minimumPrice;
private decimal maximumPrice;
private int maximumDimension;
private int targetMargin;
public Options()
{
this.IgnoreList = new ObservableCollection<string>();
}
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<string> IgnoreList
{
get
{
return this.ignoreList;
}
set
{
this.ignoreList = value;
this.OnPropertyChanged("IgnoreList");
}
}
public decimal MinimumPrice
{
get
{
return this.minimumPrice;
}
set
{
this.minimumPrice = value;
this.OnPropertyChanged("MinimumPrice");
}
}
public decimal MaximumPrice
{
get
{
return this.maximumPrice;
}
set
{
this.maximumPrice = value;
this.OnPropertyChanged("MaximumPrice");
}
}
public int MaximumDimension
{
get
{
return this.maximumDimension;
}
set
{
this.maximumDimension = value;
this.OnPropertyChanged("MaximumDimension");
}
}
public int TargetMargin
{
get
{
return this.targetMargin;
}
set
{
this.targetMargin = value;
this.OnPropertyChanged("TargetMargin");
}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
XAML是:
<Grid Name="optionsGrid">
<TextBox x:Name="minimumPrice" HorizontalAlignment="Left" Height="30" Margin="251,11,0,0" TextWrapping="Wrap" Text="{Binding MinimumPrice}" VerticalAlignment="Top" Width="74"/>
<TextBox x:Name="maximumPrice" HorizontalAlignment="Left" Height="30" Margin="251,61,0,0" TextWrapping="Wrap" Text="{Binding MaximumPrice}" VerticalAlignment="Top" Width="74"/>
<TextBox x:Name="maximumDimension" HorizontalAlignment="Left" Height="30" Margin="251,112,0,0" TextWrapping="Wrap" Text="{Binding MaximumDimension}" VerticalAlignment="Top" Width="74"/>
<TextBox x:Name="targetMargin" HorizontalAlignment="Left" Height="30" Margin="251,161,0,0" TextWrapping="Wrap" Text="{Binding TargetMargin}" VerticalAlignment="Top" Width="74"/>
<Button x:Name="cancelButton" Content="Cancel" HorizontalAlignment="Left" Margin="170,231,0,0" VerticalAlignment="Top" Width="76" Click="cancelButton_Click" Height="20"/>
<Button x:Name="okButton" Content="OK" HorizontalAlignment="Left" Margin="251,231,0,0" VerticalAlignment="Top" Width="76" Height="20" Click="okButton_Click"/>
<DataGrid x:Name="ignoreGrid" HorizontalAlignment="Left" Height="220" Margin="10,31,0,0" IsReadOnly="False" ItemsSource="{Binding IgnoreList, Mode=TwoWay}" CanUserAddRows="True" CanUserDeleteRows="True" HeadersVisibility="None" AutoGenerateColumns="False" VerticalAlignment="Top" Width="146">
<DataGrid.Columns>
<DataGridTextColumn x:Name="ignoreList" Binding="{Binding Path=.}" Width="144"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
背后代码的相关部分是:
public OptionsDialog(OptionsController options)
{
this.InitializeComponent();
Options options = new Options();
options.MaximumDimension = 77;
options.MinimumPrice = 0.50m;
options.MaximumPrice = 99.99m;
options.TargetMargin = 49;
options.IgnoreList.Add("aaa");
options.IgnoreList.Add("bbb");
this.optionsGrid.DataContext = options;
}
更新1
根据HamletHakobyan的建议更改了DataGridTextColumn定义:
<DataGridTextColumn x:Name="ignoreList" IsReadOnly="False" Binding="{Binding Path=., Mode=TwoWay}" Width="144"/>
错误已停止。但是现在当剩下编辑的单元格时,单元格只会恢复为原始值。作为一个实验,我生成了另一个TextBlock并将IgnoreList [0]绑定到此。它工作得很好。这告诉我,问题在于数据网格定义而不是源数据定义或绑定。然而,我无法向前迈进。
更新2
我注意到的其他可能相关的是,即使CanUserAddRows =&#34; True&#34;,数据网格也不会在底部显示空白行。好像网格以某种方式被锁定了。然而,通过单击一行并点击Delete键,我可以成功删除行,我可以通过代码向IgnoreList添加行。我还将调试器指向了Options类中IgnoreList的setter。当我更改单元格的值时,不会触发。不知道为什么。