-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新,我的搜索已經使我無處...
謝謝你很多!
謝謝你這樣工作。我不知道如果get和set方法沒有特別的東西(習慣於Python,我是C#中的新手),那麼Properties和Public attributs就不一樣了。謝謝 ! – Gaulois94