FabioSSA

Hi, sorry for my english. I buid a custom ListView. I want a behaviour automatic for add new row when the ItemSelected is the last item, for this behaviour I create new entity and call

ListView.Items.refresh()

all right but after this command I lost focus! the focus move to listview and if I pressed key tab wpf selected first ListViewItem!! if possible I want after command ListView.Items.refresh() selected last ListViewItem in automatic

thanks



Re: Windows Presentation Foundation (WPF) Problem with ListView.Items.refresh() and focus

benskynet

Hi,


You probably have a reference to the new ListViewItem, let's say newlistviewitem =>

listview.Items.Add(newlistviewitems);
listview.SelectedItem = newlistviewitem;
listview.Focus();


Code Snippet

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Sample.Window1"
Height="300" Width="300"
x:Name="window"
FocusManager.IsFocusScope="True"
>
<Grid>
<StackPanel>
<Button Content="Add ListViewItem" Click="btn_Click"/>
<ListView x:Name="listview"/>
</StackPanel>
</Grid>
</Window>
using System;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Sample
{
///
/// Interaction logic for Window1.xaml
///

public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
}

private void btn_Click(object sender, RoutedEventArgs e)
{
ListViewItem newlistviewitem = new ListViewItem();
newlistviewitem.Content = "I'm a new listviewitem";
listview.Items.Add(newlistviewitem);
listview.SelectedItem = newlistviewitem;
listview.Focus();
}
}
}



And you're ready.

Best regards,

Benny





Re: Windows Presentation Foundation (WPF) Problem with ListView.Items.refresh() and focus

FabioSSA

Hi,

thanks for your help but I use ListView.ItemSource and when execute:

ListViewItem newlistviewitem = new ListViewItem();

newlistviewitem.Content = Entity; //this Entity already added to IList() Property

this.Items.Add(newlistviewitem);

Raise InvalidOperationexception whit this message

"Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."





Re: Windows Presentation Foundation (WPF) Problem with ListView.Items.refresh() and focus

benskynet

You've probably bound the ListView to the IList, well, then it is not allowed to fill it this way.