NSPopUpButton, Привязка и сокращающаяся продолжительность жизни

Я пишу простую демонстрацию, чтобы удовлетворить ваши требования. Я использую Grouped ListViews:

OrderModel :

public class OrderModel
{

    public string orderName { get; set; }

    public OrderModel()
    {
    }
}

public class GroupedOrderModel : ObservableCollection
{
    public string personName { get; set; }
}

Установите источник данных:

public partial class GroupedListXaml : ContentPage
{
    private ObservableCollection grouped { get; set; }
    public GroupedListXaml ()
    {
        InitializeComponent ();
        grouped = new ObservableCollection ();
        var person1Group = new GroupedOrderModel() { personName = "   john"};
        var person2Group = new GroupedOrderModel() { personName = "   alex"};
        var person3Group = new GroupedOrderModel() { personName = "   jack"};


        person1Group.Add (new OrderModel () { orderName = "   OrderOne"});
        person1Group.Add (new OrderModel() { orderName = "   OrderTwo" });
        person1Group.Add (new OrderModel() { orderName = "   OrderThree"});
        person1Group.Add (new OrderModel() { orderName = "   OrderFour"});

        person2Group.Add (new OrderModel() { orderName = "   OrderOne"});
        person2Group.Add (new OrderModel() { orderName = "   OrderTwo"});
        person2Group.Add (new OrderModel() { orderName = "   OrderThree"});

        grouped.Add (person1Group);
        grouped.Add (person2Group);

        //Person3 without OrderModel
        grouped.Add(person3Group);

        lstView.ItemsSource = grouped;
    }
}

[ 1119] В XAML используйте ListView.GroupHeaderTemplate для настройки заголовка группы:


    

        
            
                
                    
                        

Давайте посмотрим на результат:

[ 118] Grouped ListViews

как я могу отобразить person3, если его список элементов пуст?

blockquote>

Проверить код dataSource, а Person3 не имеет [ 116].

7
задан Clokey 10 April 2009 в 14:34
поделиться

2 ответа

OK, bindings with the NSPopUpButton are a bit complicated because there are two things it needs: a binding for the values, and a binding for which one of those values is selected. What makes it even more complicated is that there are a couple of perfectly legitimate ways of doing that, and which one you choose entirely depends on your program's structure and, to some extent, personal preferences.

So, in order to get a list of values, you bind the content property. In your case, you'd probably bind this to the arrangedObjects key of an NSArrayController. In this setup, each menu item represents one object. By default, the title of the menu item is the string returned by calling description on each item in the array. If you want to use a different property for the menu title, you can also bind the contentValues array. Just make sure the key path you specify for contentValues has the key path for content as its prefix (e.g. you might use arrangedObjects for content and arrangedObjects.name for contentValues)

This will give you menu items that represent objects. What you need next is some way of identifying the selected one. There are three different bindings you can use: selectedIndex, selectedObject and selectedValue. They represent, respectively, the index of the array object that the user selected, the object value (one of the objects in the content array), and the string title of the selected item (one of the objects in the contentValues array if you bound that property).

So, in your case, you might bind selectedObject to a selectedSerialPort property on your controller class. When the user clicks the "Connect" button, you only have to refer to the selectedSerialPort property.

12
ответ дан 6 December 2019 в 10:53
поделиться

Вы можете скачать пример проекта Xcode здесь . Это как можно ближе к вашему описанию.

Важно посмотреть на связи между контроллером массива, AppController и всплывающим окном.

По сути, это просто демонстрирует, что сказал Алекс.

6
ответ дан 6 December 2019 в 10:53
поделиться