簡體   English   中英

為什么在DataGrid WPF中綁定后無法正常顯示數據

[英]Why I can't normally show my data after binding in DataGrid WPF

將ObversableCollection綁定到DataGrid之后,DataGrid無法顯示我的數據。 在此處輸入圖片說明

此圖顯示xx.xaml可以使用Staff_Show。但是此圖無法正常顯示數據。 在此處輸入圖片說明

我的程序如下:

<Grid>
    <DataGrid x:Name="DataGrid1" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False"
              ItemsSource="{Binding Staff_Show}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="姓名" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="稅前金額" Binding="{Binding TB_Sum}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

和.cs代碼:

    public ObservableCollection<Staff> Staff_Show = new ObservableCollection<Staff>();
    public TaxBefore_Sum(ObservableCollection<Staff> StaffAccept)
    {
        InitializeComponent();
        Staff_Show = StaffAccept;
        DataGrid1.DataContext = this.Staff_Show;

    }
this.Datacontext = this

嘗試這個

編輯

好,您必須實現INotifyPropertyChanged接口。

假設您的班級名稱為TaxBefore_Sum

public class TaxBefore_Sum : UserControl, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new  PropertyChangedEventArgs(propertyName));
    }
    private ObservableCollection<Staff> staff_show = new ObservableCollection<Staff>();
    public ObservableCollection<Staff> Staff_Show
    {
     get { return staff_show;}
     set { staff_show = value; this.OnPropertyChanged("Staff_Show"); }
    }
    public Taxbefore_Sum(ObservableCollection<Staff> StaffAccept)
    {
         InitializeComponent();
         this.DataContext = this;
         Staff_Show = StaffAccept;
    }

}

希望它能工作

請試試

DataGrid1.ItemsSource = this.Staff_Show;

它應該工作。 另外,您應該添加

AutoGenerateColumns="False"

在DataGrid Xaml標記中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM