【问题标题】:How can I get a DataGrid to sort it's backing ObservableCollection when the grid itself is sorted?当对网格本身进行排序时,如何让 DataGrid 对其支持的 ObservableCollection 进行排序?
【发布时间】:2011-06-21 15:23:04
【问题描述】:

我有一个绑定到对象列表的网格,SelectedIndex 绑定到一个属性,我希望这个SelectedIndex 在排序时索引更改时正常工作,但是模型中的集合没有得到排序,因此索引与模型无关。

视图中的标记:

<DataGrid Name="gridCustomers"
          ItemsSource="{Binding Customers}"
          SelectedIndex="{Binding SelectedIndex}"
          CanUserSortColumns="True"
          SelectionUnit="FullRow">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Customer" Binding="{Binding ID}" />
        <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
    </DataGrid.Columns>
</DataGrid>

模型中的代码

public ObservableCollection<Customer> Customers {
    get;
    private set;
}

private int selectedIndex;
public int SelectedIndex {
    get { return selectedIndex; }
    set {
        if (selectedIndex != value) {
            selectedIndex = value;
            OnNotifyPropertyChanged("SelectedIndex");
            OnNotifyPropertyChanged("SelectedCustomer");
        }
    }
}

public Customer SelectedCustomer {
    get { return CustomersView[selectedIndex]; }
}

我使用这种方法是因为我有“下一个/上一个”按钮和一个绑定到“x of y 个客户”的标签。 SelectedCustomer 是子控件的便利属性,在这种情况下显示不正确的对象。

【问题讨论】:

    标签: wpf binding wpfdatagrid


    【解决方案1】:

    我已经设法通过使用SelectedIndexSelectedItem 绑定属性而不是浏览列表本身来解决这个问题。

    查看:

    <DataGrid Name="gridCustomers"
                ItemsSource="{Binding Path=Customers}"
                SelectedIndex="{Binding SelectedIndex}"
                SelectedItem="{Binding SelectedCustomer}"
    

    型号:

    public ObservableCollection<Customer> Customers {
        get;
        private set;
    }
    
    private int selectedIndex;
    public int SelectedIndex {
        get { return selectedIndex; }
        set {
            if (selectedIndex != value) {
                selectedIndex = value;
                OnNotifyPropertyChanged("SelectedIndex");
                OnNotifyPropertyChanged("NavText");
            }
        }
    }
    
    private Customer selectedCustomer;
    public Customer SelectedCustomer {
        get { return selectedCustomer; }
        set {
            if (!Object.Equals(selectedCustomer, value)) {
                selectedCustomer = value;
                OnNotifyPropertyChanged("SelectedCustomer");
            }
        }
    }
    
    public string NavText {
        get {
            if (Customers.Count == 0)
                return "No Customers";
            if (SelectedIndex == -1)
                return String.Format("{0} customers", customers.Count);
            return String.Format("{0} of {1}", SelectedIndex + 1, Customers.Count);
        }
    }
    
    public void First() {
        if (Customers.Count == 0)
            return;
        SelectedIndex = 0;
    }
    
    public void Last() {
        if (Customers.Count == 0)
            return;
        SelectedIndex = Customers.Count - 1;
    }
    
    public void Next() {
        if (Customers.Count == 0 || SelectedIndex == Customers.Count - 1)
            return;
        SelectedIndex++;
    }
    
    public void Previous() {
        if (Customers.Count == 0 || SelectedIndex <= 0)
            return;
        SelectedIndex--;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-29
      • 2011-11-09
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 2013-05-09
      • 2014-12-12
      • 1970-01-01
      • 2013-12-23
      相关资源
      最近更新 更多