我有一个绑定到ObservableCollection的DataGrid的问题,如果我在OC上显式设置了一个跳过DataGrid的值[我有一个自动完成的文本框,所以当用户点击列表框条目时我将OC设置为当前选择索引到正确的AutoCompleted对象]它变为只读,如果我尝试用F2编辑它我得到这个异常:InvalidOperationException - 添加的项目不会出现在给定的索引'0'。
XAML:
<Window x:Class="LDary.Compras"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:LDary"
Title="Compras" Height="300" Width="300" Name="WinCompras">
<Window.Resources>
<CollectionViewSource Source="{Binding ElementName=WinCompras, Path=AutoCSource}" x:Key="Source" Filter="CollectionViewSource_Filter" />
<l:VisibilityToBooleanConverter x:Key="VisToBool" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="Articulos de la compra:" Padding="0,2"/>
<DataGrid Grid.Row="1" AutoGenerateColumns="False" x:Name="Lista" CanUserAddRows="True" ItemsSource="{Binding ElementName=WinCompras, Path=CompraActual}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Nombre" x:Name="Nombre">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Nombre}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding Nombre}" TextChanged="TextBox_TextChanged" Loaded="TextBox_Loaded" Unloaded="TextBox_Unloaded"/>
<Popup IsOpen="{Binding ElementName=ACLB, Path=Visibility, Mode=OneWay, Converter={StaticResource VisToBool}}">
<ListBox Visibility="Hidden" ItemsSource="{Binding Source={StaticResource Source}}" MouseLeftButtonUp="ListBox_MouseLeftButtonUp"
Focusable="False" Loaded="ListBox_Loaded" Unloaded="ListBox_Unloaded" x:Name="ACLB">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Nombre}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Popup>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Cantidad" Binding="{Binding Cantidad}" />
<DataGridTextColumn Header="Valor" Binding="{Binding Valor}" />
<DataGridTextColumn Header="Referencia" Binding="{Binding Referencia}" />
<DataGridTextColumn Header="Material" Binding="{Binding Material}" />
<DataGridTextColumn Header="Color" Binding="{Binding Color}" />
</DataGrid.Columns>
</DataGrid>
<Button Name="BtnGuardar" Content="Guardar" Padding="0,5" Grid.Row="2" />
<Button Name="BtnReset" Content="Reiniciar" Padding="0,5" Grid.Row="3" />
</Grid>
代码隐藏:
public partial class Compras : Window, INotifyPropertyChanged
{
public ColeccionArticulos CompraActual { get { return _CompraActual; } set { _CompraActual = value; NotifyChange("CompraActual"); } }
public ColeccionArticulos _CompraActual;
public ColeccionArticulos AutoCSource { get { return _AutoCSourse; } set { _AutoCSourse = value; NotifyChange("AutoCSource"); } }
public ColeccionArticulos _AutoCSourse;
private TextBox Txt;
private ListBox Lst;
public Compras()
{
AutoCSource = CentralStorage.Articulos;
CompraActual = new ColeccionArticulos();
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyChange(String Info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(Info));
}
}
private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
{
if (Txt != null)
{
Articulo Ar = e.Item as Articulo;
e.Accepted = Ar.Nombre.ToUpper().Contains(Txt.Text.ToUpper()) || Ar.Categoria.ToUpper().Contains(Txt.Text.ToUpper())
|| Ar.Material.ToUpper().Contains(Txt.Text.ToUpper()) || Ar.Observaciones.ToUpper().Contains(Txt.Text.ToUpper())
|| Ar.Referencia.ToUpper().Contains(Txt.Text.ToUpper()) || Ar.Color.ToUpper().Contains(Txt.Text.ToUpper());
}
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
CollectionViewSource CVS = (CollectionViewSource)WinCompras.FindResource("Source");
Lst.Visibility = Txt.Text != "" ? Visibility.Visible : Visibility.Hidden;
CVS.View.Refresh();
}
#region Find Template Controls
private void TextBox_Loaded(object sender, RoutedEventArgs e)
{
Txt = sender as TextBox;
}
private void TextBox_Unloaded(object sender, RoutedEventArgs e)
{
Txt = null;
}
private void ListBox_Loaded(object sender, RoutedEventArgs e)
{
Lst = sender as ListBox;
}
private void ListBox_Unloaded(object sender, RoutedEventArgs e)
{
//Lst = null;
}
#endregion
private void ListBox_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//THIS IS WHERE THE PROBLEM STARTS >.<
Articulo Ar = ((sender as ListBox).SelectedItem as Articulo);
CompraActual[Lista.SelectedIndex] = Ar.Clone() as Articulo;
Lista.CommitEdit();
}
}
public class VisibilityToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Visibility Vis = (Visibility)value;
switch (Vis)
{
case Visibility.Collapsed:
return false;
case Visibility.Hidden:
return false;
case Visibility.Visible:
return true;
default:
return false;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
任何想法?