2017-08-20 317 views
-1

我想綁定一些屬性從我的DataContext到我的DataGrid沒有成功... DataGridComboBox是空的和不可見的,然後點擊它,我有兩行而不是一個下面的源代碼。WPF綁定DataGrid

的XAML

<DataGrid Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" ItemsSource="{Binding ClassRow}" AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
    <DataGridComboBoxColumn Header="Classe" x:Name="Class_ClassName" Width="100" SelectedItemBinding="{Binding ClassName, Mode=TwoWay}" ItemsSource="{Binding ClassList}"/> 
    <DataGridTemplateColumn Header="Niveau"> 
     <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
      <TextBox x:Name="LevelTxt" Width="50" TextChanged="LevelTxtTextChanged" Text="{Binding ClassLevel, TargetNullValue={x:Static sys:String.Empty}}"/> 
      <StackPanel Orientation="Vertical"> 
       <Button x:Name="LevelUp" Content="+" Width="15" Height="15" Click="LevelUpClick" FontSize="10" VerticalContentAlignment="Top" /> 
       <Button x:Name="LevelDown" Content="-" Width="15" Height="15" Click="LevelDownClick" FontSize="12" VerticalContentAlignment="Bottom"/> 
      </StackPanel> 
      </StackPanel> 
     </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

在DataContext:

public class ClassRow 
{ 
    public String ClassName; 
    public int ClassLevel; 
} 

public class PJDataWindow : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected List<ClassRow> m_classRow; 
    protected List<String> m_classList; 

    public PJDataWindow() 
    { 
     m_classRow = new List<ClassRow>(); 
     m_classList = new List<String>(); 

     //Test 
     m_classList.Add("Classe1"); 
     m_classRow.Add(new ClassRow { ClassName = "Classe1", ClassLevel = 2 }); 

     OnPropertyChanged("ClassList"); 
     OnPropertyChanged("ClassRow"); 
    } 

    protected void OnPropertyChanged(string name) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); 
    } 

    public List<ClassRow> ClassRow 
    { 
     get 
     { 
      return m_classRow; 
     } 

     set 
     { 
      m_classRow = value; 
      OnPropertyChanged("ClassRow"); 
     } 
    } 

    public List<String> ClassList { get => m_classList; set { m_classList = value; OnPropertyChanged("ClassList"); } } 
} 

我在WPF新,我的搜索已經使我無處...

謝謝你很多!

回答

1

有3個問題(到目前爲止)。

最簡單的問題是兩行而不是一行。額外的行是新的項目行。把它關閉,像這樣...

<DataGrid Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" ItemsSource="{Binding ClassRow}" AutoGenerateColumns="False" Margin="40" CanUserAddRows="False"> 

接下來的問題是空的文本字段。你只能綁定到屬性而不是字段。因此,爲了解決這個問題,行中的字段對象屬性。

public class ClassRow 
{ 
    public String ClassName { get; set; } 
    public int ClassLevel { get; set; } 
} 

最後,組合框爲空的原因是項目源未綁定到數據上下文。數據網格列在可視化樹之外。所以它找不到來源。像這樣修復它。

<FrameworkElement x:Name="dummyElement" Visibility="Collapsed"/> 
    <DataGrid Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" ItemsSource="{Binding ClassRow}" AutoGenerateColumns="False" Margin="40" CanUserAddRows="False" x:Name="dataGrid"> 
     <DataGrid.Columns> 
      <DataGridComboBoxColumn Header="Classe" x:Name="Class_ClassName" Width="100" SelectedItemBinding="{Binding ClassName, Mode=TwoWay}" ItemsSource="{Binding Source={x:Reference dummyElement}, Path=DataContext.ClassList}"/> 
+0

謝謝你這樣工作。我不知道如果get和set方法沒有特別的東西(習慣於Python,我是C#中的新手),那麼Properties和Public attributs就不一樣了。謝謝 ! – Gaulois94