简体   繁体   English

在tabitems中加载列表框-使用哪个事件?

[英]Loading listboxes inside tabitems - which event to use?

Lets say I have an application which comprises one window. 可以说我有一个包含一个窗口的应用程序。 Inside the window is a tabcontrol, with three tabitems. 窗口内部是一个tabcontrol,具有三个tabitems。 Inside each tabitem is a usercontrol. 每个选项中都有一个用户控件。

In one tab I have Add color. 在一个标签中,我有添加颜色。 In the next tab I have add Fruit. 在下一个标签中,我添加了Fruit。 In the third tab I have relationships, where the user can add links between the fruit and the colors. 在第三个选项卡中,我具有关系,用户可以在其中添加水果和颜色之间的链接。 This relationship is displayed in a listbox like: 此关系显示在如下列表框中:

Apple > Red
Pear  > Green

Below this in the same tab I have missing fruits listbox and a missing colors listbox... ie fruits or colors that have been added but not linked in the relationship. 在同一标签下的该位置下,我缺少水果列表框和颜色列表框...即水果或已添加但未在关系中链接的颜色。 I should explain that this data is all stored in three seperate textfiles, fruits.txt, colors.txt and relationships.txt. 我应该解释一下,这些数据都存储在三个单独的文本文件中,fruits.txt,colors.txt和Relationships.txt。

The problem I have is as follows. 我的问题如下。 At the moment the populating of the listboxes etc is on Usercontrol_loaded event. 目前,列表框等的填充处于Usercontrol_loaded事件上。 It doesn't matter for fruit/colors, as after the add button is clicked, the list reloads. 水果/颜色无关紧要,因为单击添加按钮后,列表会重新加载。 The problem is in the relationship screen. 问题出在关系屏幕上。

Say the user runs the program, and clicks on the relationship tab to see what is linked. 假设用户运行该程序,然后单击“关系”选项卡以查看链接的内容。 We'll say that the above example was already in the textfile. 我们将说上面的示例已经在文本文件中。 So that relationship shows up - and no missing fruits. 这样,这种关系就显现出来了,而且也不会丢失任何成果。 The user then clicks the fruit tab and adds a fruit and then the color tab and adds a color. 然后,用户单击“水果”选项卡并添加水果,然后单击“颜色”选项卡并添加颜色。 Then moves to the relationship tab. 然后移至“关系”选项卡。 The usercontrol_loaded event has already occured, so these two new additions do not show in the listboxes. usercontrol_loaded事件已经发生,因此这两个新添加项不会显示在列表框中。

If I move the code to GotFocus event, the user can't make selection in any listbox because it is constantly loading, as clicking fires the event. 如果我将代码移到GotFocus事件,则用户无法在任何列表框中进行选择,因为它会不断加载,因为单击会触发该事件。

Is there any other event or way I could have this refresh JUST when the tab has been "switched to", other than providing a refresh button? 除了提供“刷新”按钮以外,是否有其他事件或方式可以使此选项卡“切换到”?

Thanks for reading. 谢谢阅读。

... ...

Edit 1: If I databind to a list of missing fruits which I build in the codebehind, I still have the same problem. 编辑1:如果我将数据绑定到我在后面的代码中构建的缺少水果的列表,则我仍然有同样的问题。 I have to reload this list everytime they go jump off this tab and come back to it (because potentially they've added a fruit or a color, which is now missing). 每当他们离开此标签并返回到该标签时,我都必须重新加载此列表(因为他们可能添加了一种水果或一种颜色,现在已经缺少了)。

You need to understand how MVVM and change notification works. 您需要了解MVVM和更改通知的工作方式。 You don't need to use events or code-behind at all if you're tracking all of this stuff in observable collections in view model classes that are bound to the UI. 你并不需要,如果你跟踪所有的这些东西在视图模型类观察集合绑定到用户界面使用事件或代码隐藏在所有

As wwosik suggests, create a class that exposes public Colors , Fruits , and Relationships properties that are all observable collections. 如wwosik所建议,创建一个类以公开所有可观察的集合的公共ColorsFruitsRelationships属性。 As s/he didn't suggest but probably should have, also expose public MissingColors and MissingFruits observable collections. 正如他/她没有建议,但应该建议的那样,还公开了公开的MissingColorsMissingFruits可观察的集合。 Finally, expose public SelectedColor and SelectedFruit properties. 最后,公开公共SelectedColorSelectedFruit属性。

Create bound controls: 创建绑定控件:

<ListBox ItemsSource="{Binding Colors}" SelectedItem="{Binding SelectedColor}"/>
<ListBox ItemsSource="{Binding Fruits}" SelectedItem="{Binding SelectedFruit}"/>
<ListBox ItemsSource="{Binding Relationships}"/>
<ListBox ItemsSource="{Binding MissingColors}"/>
<ListBox ItemsSource="{Binding MissingFruits}"/>

Implement an AddRelationship method that adds a new relationship consisting of the SelectedColor and SelectedFruit . 实现一个AddRelationship方法,该方法添加一个新的关系,该关系由SelectedColorSelectedFruit It should also remove the color and fruit from the respective MissingColors and MissingFruits collections. 它还应从各自的MissingColorsMissingFruits集合中删除颜色和水果。 Create a command that calls this method and bind it to something in the UI. 创建一个调用此方法的命令,并将其绑定到UI中的某些内容。

That's it. 而已。 It doesn't matter what tab anything is on. 哪个选项卡都没有关系。 It doesn't matter what order the user views things in. When the user adds a relationship, or a fruit, or a color, the UI will get updated. 用户查看事物的顺序无关紧要。当用户添加关系,水果或颜色时,UI将会更新。

You need to bind your listboxes to the data from your ViewModel. 您需要将列表框绑定到ViewModel中的数据。 This way the WPF framework overtakes the detection of the moment when to update the data. 这样,WPF框架就超过了何时更新数据的检测。

WPF is not WinForms. WPF不是WinForms。 You don't populate ListBoxes by yourself. 您不会自己填充ListBoxes。

You have 你有

class MyData{
   ObservableCollection<Color> Colors;
   ObservableCollection<Fruit> Fruits;
   ObservableCollection<Pairs> Pairs;

   public void MatchCurrentSelection(){
        var selectedColor = CollectionViewSource.GetDefaultView(Colors).CurrentItem;
        var selectedFruit = CollectionViewSource.GetDefaultView(Fruits).CurrentItem;
        if(selectedColor != null && selectedFruit != null){
             Colors.Remove(selectedColor);
             Fruits.Remove(selectedFruit);
             Pairs.Add(new Pair(selectedColor, selectedFruit));
        }
   }
} 

ListBoxes 列表框

<ListBox ItemsSource="{Binding Colors}" IsSynchronizedWithCurrentItem="true"/>
<ListBox ItemsSource="{Binding Fruits}" IsSynchronizedWithCurrentItem="true"/>
<ListBox ItemsSource="{Binding Pairs}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM