WPF绑定DataGrid

时间:2017-08-20 17:37:56

标签: c# wpf xaml datagrid

我试图将我的DataContext中的一些属性绑定到我的DataGrid但没有成功... DataGridComboBox在点击之前是空的并且是不可见的,我有两行而不是一行用于以下源代码。

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 个答案:

答案 0 :(得分: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}"/>