当网格本身排序时,如何让 DataGrid 对其支持的 ObservableCollection 进行排序?

发布于 2024-11-16 04:44:03 字数 1225 浏览 3 评论 0原文

我有一个绑定到对象列表的网格,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 是子控件的一个便利属性,在本例中显示了不正确的对象。

I have a grid bound to a list of objects, the SelectedIndex is bound to a property, I wish this SelectedIndex which works correctly as the index changes on sorting, however the collection within the model does not get sorted, so the index does not relate to the model.

Markup in view:

<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>

Code in model

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]; }
}

I'm using this approach because I have "next/previous" buttons and a label binding to "x of y customers". The SelectedCustomer is a convenience property for a child control which in this case shows the incorrect object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-11-23 04:44:03

我设法通过使用 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--;
}

I've managed to solve this by using both SelectedIndex and SelectedItem binding properties and not navigating through the list itself.

View:

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

Model:

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--;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文