1
private void cmbEmployee_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string employee = (e.AddedItems[0] as ComboBoxItem).Content as string;

    dgFake.ItemsSource = newdal2.SelectUser(employee).Tables[0].DefaultView;
}

このメソッドは、この従業員がコンボ ボックスからクリックされたときに特定の従業員によって WPF ウィンドウ フォームのデータ グリッドにデータを入力しますが、最初の従業員の後に別の従業員をクリックすると、データ グリッドは更新されず、その従業員の最初の下のデータ。

これは WPF Xaml Windows フォーム上にあり、DataGridView ではありません。私はすでにこれらを試しましたが、どれもうまくいきませんでした:

dgFake.Items.Refresh();
dgFake.Items.Remove(); //Required a remove item passed to the method, so too specific
dgFake.Itemssource = "";
4

1 に答える 1

3

一般に、WPF を使用すると、 UI 要素ではなくデータを操作します。したがって、コレクション プロパティをプロパティに追加した後は、コレクション プロパティを簡単に操作できます。BindingDataGrid.ItemsSource

XAML の場合:

<DataGrid ItemsSource="{Binding YourCollection}" ... />

次にコードで:

YourCollection.Clear();

または項目を変更するには:

YourCollection = someNewCollection;

このようにデータを変更した後に が自動的に更新されるようにするには、INotifyPropertyChangedインターフェイスを実装する必要があります。DataGrid


更新 >>>

コメントへの応答: 「XAML コードを追加しました。XAML コードで 'YourCollection' について話すとき、ここに何を入れる必要がありますか?」:

Bindableコードでコレクション プロパティを作成する必要があります。これはDependencyProperty、コード ビハインド内の か、INotifyPropertyChangedインターフェイスを実装する CLR プロパティのいずれかです。通常、UI にデータベース要素を表示することはありません。代わりに、必要なプロパティを使用してオブジェクト クラスを定義することを好みます。

public static DependencyProperty EmployeesProperty = DependencyProperty.Register(
    "Employees", typeof(ObservableCollection<Employee>), typeof(YourUserControl));

public ObservableCollection<Employee> Employees
{
    get { return (ObservableCollection<Employee>)GetValue(EmployeesProperty); }
    set { SetValue(EmployeesProperty, value); }
}

次に、cmbEmployee_SelectionChangedハンドラー メソッドで、コレクション プロパティの値を次のように更新できます。

private void cmbEmployee_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Employees = new ObservableCollection<Employee>();
    string employee = (e.AddedItems[0] as ComboBoxItem).Content as string;
    foreach (DataRow row in newdal2.SelectUser(employee).Tables[0].Rows)
    {
        Employees.Add(new Employee(row.Id, row.Name, row.Whatever));
    }
    Employees = newdal2.SelectUser(employee).Tables[0].DefaultView;
}
于 2013-11-11T11:52:16.127 に答える