2010-08-11 12 views
6

bir InvalidOperationException alıyorum ('AddNew veya EditItem işlemi sırasında' DeferRefresh 'izin verilmiyor). Tüm gösterdiğim öğeler, aynı listedeki diğer bir öğeye bir başvuru var, bu yüzden bu, combobox için kullanıyorum. Datagrid ile aynı koleksiyona bağlı. Üzerinde çalıştığım uygulamam .NET 3.5'i hedefliyor, ancak datagrid'in yerleşik olması nedeniyle .NET 4'te tam olarak aynı olan bir örneği bir araya getirdik. İşte datagrid öğelerinin kodu şöyledir:WPF DataGrid ComboBox, bir birleşik giriş sütununun değerini düzenlemeye çalıştığımda InvalidOperationException neden

public class TestItem : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    private void RaisePropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    private int m_ID; 
    private string m_Name; 
    private int m_OppositeID; 

    public int ID 
    { 
     get { return m_ID; } 
     set 
     { 
      m_ID = value; 
      RaisePropertyChanged("ID"); 
     } 
    } 
    public string Name 
    { 
     get { return m_Name; } 
     set 
     { 
      m_Name = value; 
      RaisePropertyChanged("Name"); 
     } 
    } 
    public int OppositeID 
    { 
     get { return m_OppositeID; } 
     set 
     { 
      m_OppositeID = value; 
      RaisePropertyChanged("OppositeID"); 
     } 
    } 

    public TestItem(int id, string name, int oppID) 
    { 
     ID = id; 
     Name = name; 
     OppositeID = oppID; 
    } 
} 
sonunda benim xaml

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    private void RaisePropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    private ObservableCollection<TestItem> m_Items; 

    public ObservableCollection<TestItem> Items 
    { 
     get { return m_Items; } 
     set 
     { 
      m_Items = value; 
      RaisePropertyChanged("Items"); 
     } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = this; 

     Items = new ObservableCollection<TestItem>(); 

     Items.Add(new TestItem(0, "Fixed", 0)); 
     Items.Add(new TestItem(1, "Left Side", 2)); 
     Items.Add(new TestItem(2, "Right Side", 1)); 
    } 
} 

ve: Eğer sunabilir herhangi bir yardım için şimdiden

<Window x:Class="DataGrid_Combo_Test.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <DataGrid ItemsSource="{Binding Path=Items}" AutoGenerateColumns="False"> 
      <DataGrid.Resources> 
       <Style x:Key="ItemsSourceStyle" TargetType="ComboBox"> 
        <Setter Property="ItemsSource" Value="{Binding Path=DataContext.Items, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/> 
       </Style> 
      </DataGrid.Resources> 
      <DataGrid.Columns> 
       <DataGridTextColumn Binding="{Binding Path=ID}" Header="ID" Width="*"/> 
       <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="*"/> 
       <DataGridComboBoxColumn Header="Opposite Item" Width="*" DisplayMemberPath="Name" SelectedValuePath="ID" SelectedValueBinding="{Binding Path=OppositeID}" ElementStyle="{StaticResource ItemsSourceStyle}" EditingElementStyle="{StaticResource ItemsSourceStyle}"/> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
</Window> 

Teşekkür

Bu benim penceresinde kodudur!

+1

Bu, Connect'te bildirilen sorunlarla bağlantılı gibi görünüyor (https://connect.microsoft.com/VisualStudio/feedback/details/591125/datagrid-exception-on-validation-failure-deferrefresh-is-not-allowed) ve MSDN Forumları (http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/187b2b8f-d403-4bf3-97ad-7f93b4385cdf/); ama bu en zarif geçici çözüm! –

+0

Bu sorunla karşılaştım çünkü DataGrid ve DataGridComboBoxColumn'umu aynı koleksiyona yanlışlıkla bağladım. – Maxence

cevap

3

Bu sorunun nasıl giderileceğini öğrendim.

benim Window.Resources böyle bir CollectionViewSource oluşturuldu: CollectionView veya DataGridXYZ.Items üzerinde Refersh çağırmadan önce

<DataGridComboBoxColumn Header="Opposite Item" Width="*" DisplayMemberPath="Name" SelectedValuePath="ID" SelectedValueBinding="{Binding Path=OppositeID}" ItemsSource="{Binding Source={StaticResource itemSource}}"/> 
2

deneyin aşağıdaki dizisi:

<Window.Resources> 
    <CollectionViewSource x:Key="itemSource" Source="{Binding Path=Items}"/> 
</Window.Resources> 

Ardından şu benim combobox sütun tanımını değiştirdi

( ) Benim için çalıştı.
+0

Gönderilen kod, sorunun kısa bir gösterimi idi. Gerçek uygulama mvvm'yi kullanır ve bu yaklaşım, olay işleyicileri ve benzerlerini eklemek anlamına gelir. – aalex675