簡體   English   中英

WPF-根據其他控件更改綁定數據

[英]WPF - changing bound data according to other controls

我正在嘗試使用WPF,因此我創建了一些測試窗口以了解其運行情況。 我有一個窗口,其中包含一個帶有一些選項的組合框,並且在該窗口中有一個數據網格,該數據網格綁定到組合框所選項目的列表的屬性(意味着當您在組合框中選擇一個項目時,數據相應地更新網格)。

<DataGrid Grid.Row="1" AutoGenerateColumns="True"
          ItemsSource="{Binding ElementName=comboBoxPeople, 
                                Path=SelectedItem.OrdersList}"/>

我在窗口中添加了一個CheckBox和一個TextBox,我想使用它們來過濾數據網格中的某些行。 復選框確定是否根本有任何過濾,並且過濾本身根據TextBox中的文本完成。

如何使用CheckBox和TextBox過濾DataGrid的行? 我知道我可以使用MultiBinding制作一個MultiValueConverter並返回想要用於DataGrid的新ItemsSource,但是我正在尋找其他解決方案。

allItems您可以將包含DataGrid-Lines的對象的Filter-Properties綁定到CheckBox和TextBox。 每次更新這些屬性時,您也會更新過濾。 此外,您還實現INotifyPropertyChanged接口,並在每次DataGrid-Lines列表更改時引發PropertyChanged事件。

您綁定到的類如下所示:

class Class1 : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
            private List<OrderItem> allItems;

    private string textBoxValue;
    public string TextBoxValue
    {
        get { return textBoxValue; }
        set
        {
            textBoxValue = value;
            updateList();
        }
    }

    private List<OrderItem> orderItems;
    private List<OrderItem> OrderItems
    {
        get { return orderItems; }
        set
        {
            orderItems= value;
            PropertyChanged(this, new PropertyChangedEventArgs("OrderItems"));
        }
    }

    private void updateList()
    {
        List<OrderItem> newList = new List<OrderItem>();
        //update the List
        foreach (OrderItem orderItem in allItems)
        {
            if (orderItem[name] == textBoxValue) newList.Add(orderItem);
        }
        OrderItems= newList;
    }
}

暫無
暫無

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

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