开发者

WPF Listbox items are cleared but reappear on restart

I have a listbox in Wpf. I directly add items to this listbox. I added a clear button which runs the items.clear() command and it deletes all the items. However when restarting the app, all the items are back in the list! Are they stored somewhere and if so, how can I disable this?

Another question: Can a datatemplate be applied to a listbox in which items are directly added and not bound?

Code example:

<ListBox x:Name="Results" Margin="222,31,8,44" /开发者_开发知识库>

Midiinput.ChannelMessageReceived += delegate(object sender, ChannelMessageEventArgs e)
{
    string result = e.Message.MessageType.ToString() + " " + e.Message.Command.ToString() + " " + e.Message.Data1.ToString() + " " + e.Message.Data2.ToString();
    Results.Items.Add(result); Clearbutton.IsEnabled = true;
};


the listbox items is not persisted (as any other property in fact). How do you populate your listbox ? is there any backend storage ?

I guess that your application simply have some items declared in the markup and when the application starts, the list items are parsed.


If the items return when you restart your app, I would think one of the following is true: 1) You are populating the listbox at DESIGN time by using the Items property and entering strings into the collection by hand. 2) You are running code that is populating the list from a storage location, either in the code or in a file.

-C


For your second question

Can a datatemplate be applied to a listbox in which items are directly added and not bound?

The ItemTemplate of a ListBox is copied to the ContentTemplate of a ListBoxItem during UI generation. However, when adding the ListBoxItems directly, ItemTemplate is ignored for items already of the ItemsControl's container type (ListBoxItem). So you'll have to use the ContentTemplate of ListBoxItem instead. Here's an example

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock Foreground="Green" Text="{Binding}"/>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBoxItem Content="Item 1"/>
    <ListBoxItem Content="Item 2"/>
    <ListBoxItem Content="Item 3"/>
</ListBox>

For your problem with items that are re-appearing when re-starting your app:

I'd try to find all the places where you add items to your ListBox in code-behind (search for Results.Items.Add). Add a breakpoint to each of those places to see in the debugger when you're adding them. The ListBox itself won't have any memory of how it looked when it closed unless you implement this.

If this doesn't work, try to change the name of your ListBox to ResultsTest or something, compile and fix all the errors you get by commenting out all the code that is referencing the ListBox and run the program again. This should leave you with no items in the ListBox. After this, start to uncomment your code, step by step until you find the place of the error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜