WPF: how to auto scroll to my first ListViewItem when form load
So i am build simple Clipboard
manager.
Every ListViewItem
come from Clipboard.GetText
and my application minimize to Tray
and when double click on its Icon
the application jump and i want to focus become on the first ListViewItem
in order to be able to navigate with Up & Down
arrows.
This is my ListView
:
ListView myListView;
Model List:
public ObservableCollection<string> Clipboards
Window Loaded event:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (viewModel.Clipboards.Count != 0)
myListView.ScrollIntoView(myListView.Items[0]);
myListView.Focus();
}
So currently when i open int the first time the application, the Focus
is not on any ListViewItem
and in the next time the focus is on the last selected/click ListViewItem
and not on the first one.
wpf listview focus
add a comment |
So i am build simple Clipboard
manager.
Every ListViewItem
come from Clipboard.GetText
and my application minimize to Tray
and when double click on its Icon
the application jump and i want to focus become on the first ListViewItem
in order to be able to navigate with Up & Down
arrows.
This is my ListView
:
ListView myListView;
Model List:
public ObservableCollection<string> Clipboards
Window Loaded event:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (viewModel.Clipboards.Count != 0)
myListView.ScrollIntoView(myListView.Items[0]);
myListView.Focus();
}
So currently when i open int the first time the application, the Focus
is not on any ListViewItem
and in the next time the focus is on the last selected/click ListViewItem
and not on the first one.
wpf listview focus
Have you tried using Focus Manager for this? Also if the ListView is bound to a ViewModel then you could control if there is anything selected without handling the code behind or relying on puny loaded events.
– XAMlMAX
Jan 2 at 9:10
Can i have simple code example of what you talking ?
– user979033
Jan 2 at 11:35
Sure.<ListView ItemsSource="{Binding Clipboards}" SelectedItem="{Binding SelectedClipboard}" Name="lst" FocusManager.FocusedElement="{Binding ElementName=lst}"/>
.
– XAMlMAX
Jan 2 at 12:49
add a comment |
So i am build simple Clipboard
manager.
Every ListViewItem
come from Clipboard.GetText
and my application minimize to Tray
and when double click on its Icon
the application jump and i want to focus become on the first ListViewItem
in order to be able to navigate with Up & Down
arrows.
This is my ListView
:
ListView myListView;
Model List:
public ObservableCollection<string> Clipboards
Window Loaded event:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (viewModel.Clipboards.Count != 0)
myListView.ScrollIntoView(myListView.Items[0]);
myListView.Focus();
}
So currently when i open int the first time the application, the Focus
is not on any ListViewItem
and in the next time the focus is on the last selected/click ListViewItem
and not on the first one.
wpf listview focus
So i am build simple Clipboard
manager.
Every ListViewItem
come from Clipboard.GetText
and my application minimize to Tray
and when double click on its Icon
the application jump and i want to focus become on the first ListViewItem
in order to be able to navigate with Up & Down
arrows.
This is my ListView
:
ListView myListView;
Model List:
public ObservableCollection<string> Clipboards
Window Loaded event:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (viewModel.Clipboards.Count != 0)
myListView.ScrollIntoView(myListView.Items[0]);
myListView.Focus();
}
So currently when i open int the first time the application, the Focus
is not on any ListViewItem
and in the next time the focus is on the last selected/click ListViewItem
and not on the first one.
wpf listview focus
wpf listview focus
edited Jan 2 at 5:54
user979033
asked Jan 2 at 5:48
user979033user979033
4762726
4762726
Have you tried using Focus Manager for this? Also if the ListView is bound to a ViewModel then you could control if there is anything selected without handling the code behind or relying on puny loaded events.
– XAMlMAX
Jan 2 at 9:10
Can i have simple code example of what you talking ?
– user979033
Jan 2 at 11:35
Sure.<ListView ItemsSource="{Binding Clipboards}" SelectedItem="{Binding SelectedClipboard}" Name="lst" FocusManager.FocusedElement="{Binding ElementName=lst}"/>
.
– XAMlMAX
Jan 2 at 12:49
add a comment |
Have you tried using Focus Manager for this? Also if the ListView is bound to a ViewModel then you could control if there is anything selected without handling the code behind or relying on puny loaded events.
– XAMlMAX
Jan 2 at 9:10
Can i have simple code example of what you talking ?
– user979033
Jan 2 at 11:35
Sure.<ListView ItemsSource="{Binding Clipboards}" SelectedItem="{Binding SelectedClipboard}" Name="lst" FocusManager.FocusedElement="{Binding ElementName=lst}"/>
.
– XAMlMAX
Jan 2 at 12:49
Have you tried using Focus Manager for this? Also if the ListView is bound to a ViewModel then you could control if there is anything selected without handling the code behind or relying on puny loaded events.
– XAMlMAX
Jan 2 at 9:10
Have you tried using Focus Manager for this? Also if the ListView is bound to a ViewModel then you could control if there is anything selected without handling the code behind or relying on puny loaded events.
– XAMlMAX
Jan 2 at 9:10
Can i have simple code example of what you talking ?
– user979033
Jan 2 at 11:35
Can i have simple code example of what you talking ?
– user979033
Jan 2 at 11:35
Sure.
<ListView ItemsSource="{Binding Clipboards}" SelectedItem="{Binding SelectedClipboard}" Name="lst" FocusManager.FocusedElement="{Binding ElementName=lst}"/>
.– XAMlMAX
Jan 2 at 12:49
Sure.
<ListView ItemsSource="{Binding Clipboards}" SelectedItem="{Binding SelectedClipboard}" Name="lst" FocusManager.FocusedElement="{Binding ElementName=lst}"/>
.– XAMlMAX
Jan 2 at 12:49
add a comment |
2 Answers
2
active
oldest
votes
It looks like you have your ViewModel in the code behind. This is not good MVVM standard.
Maybe this could be helpful
How can I set the focus to a ListBox properly on load if it uses databinding?
add a comment |
From what I see you are not focusing any ListViewItem
but the ListView
itself. I think this is your mistake. To focus the item you have to get it's container. The objects in the ItemsSource
are actually the data itself and no the UIElement
to render. To draw this data or add it to the visual tree for rendering, the ItemsControl
will generate a container for the data e.g. a ListViewItem
. Only the UIElement can receive focus, that's why the UIElement
exposes the Focus() method. You have to use the ItemContainerGenarator
to retrieve this container for your data:
ListView myListView;
(myListView.ItemsPanel as VirtualizingPanel)?.BringIndexIntoViewPublic(0);
myListView.Dispatcher.Invoke(new Action(
() =>
{
ListBoxItem dataContainer = myListView.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;
dataContainer?.Focus();
}), DispatcherPriority.ContextIdle);
This example will move the focus to the first element in the ListView.
That wouldn't (or possibly wouldn't) work if you are using virtualization (which is the default) since containers get destroyed when scrolled out of view and you'll get a null exception. You'd have to scroll item 0 into view first, then wait for the container to be generated. Only then can you set focus to it.
– SledgeHammer
Jan 2 at 18:56
This time you are right. When virtualization is used we have to take care that the requested item container is generated before we can access it. This time your comment has some value. This is how we do it. That's professional behavior.
– MacPowder
Jan 2 at 21:00
"this time"? Do I know you? lol
– SledgeHammer
Jan 2 at 21:06
Yeah, feels like you are stalking me. I am that guy that you are trying to tell to manage my controls from within the view model and that code-behind free files are mandatory to MVVM. But no problem.
– MacPowder
Jan 2 at 21:10
oh.. I didn't pay attention to the name... and no, that's not what I said at all... you were too busy going on your rant to read I guess lol... but yes, I'll stick to my statement about the 2nd part... if you are using code behind you aren't packaging stuff correctly. It's simply not needed and its poor design to split the logic. I'll also stick to my other point about your approach being bad since you are tightly coupling a bunch of things. But hey... you work at BMW, so what do I know lol...
– SledgeHammer
Jan 2 at 21:21
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001793%2fwpf-how-to-auto-scroll-to-my-first-listviewitem-when-form-load%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like you have your ViewModel in the code behind. This is not good MVVM standard.
Maybe this could be helpful
How can I set the focus to a ListBox properly on load if it uses databinding?
add a comment |
It looks like you have your ViewModel in the code behind. This is not good MVVM standard.
Maybe this could be helpful
How can I set the focus to a ListBox properly on load if it uses databinding?
add a comment |
It looks like you have your ViewModel in the code behind. This is not good MVVM standard.
Maybe this could be helpful
How can I set the focus to a ListBox properly on load if it uses databinding?
It looks like you have your ViewModel in the code behind. This is not good MVVM standard.
Maybe this could be helpful
How can I set the focus to a ListBox properly on load if it uses databinding?
answered Jan 2 at 7:12


BjornBjorn
187
187
add a comment |
add a comment |
From what I see you are not focusing any ListViewItem
but the ListView
itself. I think this is your mistake. To focus the item you have to get it's container. The objects in the ItemsSource
are actually the data itself and no the UIElement
to render. To draw this data or add it to the visual tree for rendering, the ItemsControl
will generate a container for the data e.g. a ListViewItem
. Only the UIElement can receive focus, that's why the UIElement
exposes the Focus() method. You have to use the ItemContainerGenarator
to retrieve this container for your data:
ListView myListView;
(myListView.ItemsPanel as VirtualizingPanel)?.BringIndexIntoViewPublic(0);
myListView.Dispatcher.Invoke(new Action(
() =>
{
ListBoxItem dataContainer = myListView.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;
dataContainer?.Focus();
}), DispatcherPriority.ContextIdle);
This example will move the focus to the first element in the ListView.
That wouldn't (or possibly wouldn't) work if you are using virtualization (which is the default) since containers get destroyed when scrolled out of view and you'll get a null exception. You'd have to scroll item 0 into view first, then wait for the container to be generated. Only then can you set focus to it.
– SledgeHammer
Jan 2 at 18:56
This time you are right. When virtualization is used we have to take care that the requested item container is generated before we can access it. This time your comment has some value. This is how we do it. That's professional behavior.
– MacPowder
Jan 2 at 21:00
"this time"? Do I know you? lol
– SledgeHammer
Jan 2 at 21:06
Yeah, feels like you are stalking me. I am that guy that you are trying to tell to manage my controls from within the view model and that code-behind free files are mandatory to MVVM. But no problem.
– MacPowder
Jan 2 at 21:10
oh.. I didn't pay attention to the name... and no, that's not what I said at all... you were too busy going on your rant to read I guess lol... but yes, I'll stick to my statement about the 2nd part... if you are using code behind you aren't packaging stuff correctly. It's simply not needed and its poor design to split the logic. I'll also stick to my other point about your approach being bad since you are tightly coupling a bunch of things. But hey... you work at BMW, so what do I know lol...
– SledgeHammer
Jan 2 at 21:21
|
show 1 more comment
From what I see you are not focusing any ListViewItem
but the ListView
itself. I think this is your mistake. To focus the item you have to get it's container. The objects in the ItemsSource
are actually the data itself and no the UIElement
to render. To draw this data or add it to the visual tree for rendering, the ItemsControl
will generate a container for the data e.g. a ListViewItem
. Only the UIElement can receive focus, that's why the UIElement
exposes the Focus() method. You have to use the ItemContainerGenarator
to retrieve this container for your data:
ListView myListView;
(myListView.ItemsPanel as VirtualizingPanel)?.BringIndexIntoViewPublic(0);
myListView.Dispatcher.Invoke(new Action(
() =>
{
ListBoxItem dataContainer = myListView.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;
dataContainer?.Focus();
}), DispatcherPriority.ContextIdle);
This example will move the focus to the first element in the ListView.
That wouldn't (or possibly wouldn't) work if you are using virtualization (which is the default) since containers get destroyed when scrolled out of view and you'll get a null exception. You'd have to scroll item 0 into view first, then wait for the container to be generated. Only then can you set focus to it.
– SledgeHammer
Jan 2 at 18:56
This time you are right. When virtualization is used we have to take care that the requested item container is generated before we can access it. This time your comment has some value. This is how we do it. That's professional behavior.
– MacPowder
Jan 2 at 21:00
"this time"? Do I know you? lol
– SledgeHammer
Jan 2 at 21:06
Yeah, feels like you are stalking me. I am that guy that you are trying to tell to manage my controls from within the view model and that code-behind free files are mandatory to MVVM. But no problem.
– MacPowder
Jan 2 at 21:10
oh.. I didn't pay attention to the name... and no, that's not what I said at all... you were too busy going on your rant to read I guess lol... but yes, I'll stick to my statement about the 2nd part... if you are using code behind you aren't packaging stuff correctly. It's simply not needed and its poor design to split the logic. I'll also stick to my other point about your approach being bad since you are tightly coupling a bunch of things. But hey... you work at BMW, so what do I know lol...
– SledgeHammer
Jan 2 at 21:21
|
show 1 more comment
From what I see you are not focusing any ListViewItem
but the ListView
itself. I think this is your mistake. To focus the item you have to get it's container. The objects in the ItemsSource
are actually the data itself and no the UIElement
to render. To draw this data or add it to the visual tree for rendering, the ItemsControl
will generate a container for the data e.g. a ListViewItem
. Only the UIElement can receive focus, that's why the UIElement
exposes the Focus() method. You have to use the ItemContainerGenarator
to retrieve this container for your data:
ListView myListView;
(myListView.ItemsPanel as VirtualizingPanel)?.BringIndexIntoViewPublic(0);
myListView.Dispatcher.Invoke(new Action(
() =>
{
ListBoxItem dataContainer = myListView.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;
dataContainer?.Focus();
}), DispatcherPriority.ContextIdle);
This example will move the focus to the first element in the ListView.
From what I see you are not focusing any ListViewItem
but the ListView
itself. I think this is your mistake. To focus the item you have to get it's container. The objects in the ItemsSource
are actually the data itself and no the UIElement
to render. To draw this data or add it to the visual tree for rendering, the ItemsControl
will generate a container for the data e.g. a ListViewItem
. Only the UIElement can receive focus, that's why the UIElement
exposes the Focus() method. You have to use the ItemContainerGenarator
to retrieve this container for your data:
ListView myListView;
(myListView.ItemsPanel as VirtualizingPanel)?.BringIndexIntoViewPublic(0);
myListView.Dispatcher.Invoke(new Action(
() =>
{
ListBoxItem dataContainer = myListView.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;
dataContainer?.Focus();
}), DispatcherPriority.ContextIdle);
This example will move the focus to the first element in the ListView.
edited Jan 2 at 23:39
answered Jan 2 at 13:04


MacPowderMacPowder
1105
1105
That wouldn't (or possibly wouldn't) work if you are using virtualization (which is the default) since containers get destroyed when scrolled out of view and you'll get a null exception. You'd have to scroll item 0 into view first, then wait for the container to be generated. Only then can you set focus to it.
– SledgeHammer
Jan 2 at 18:56
This time you are right. When virtualization is used we have to take care that the requested item container is generated before we can access it. This time your comment has some value. This is how we do it. That's professional behavior.
– MacPowder
Jan 2 at 21:00
"this time"? Do I know you? lol
– SledgeHammer
Jan 2 at 21:06
Yeah, feels like you are stalking me. I am that guy that you are trying to tell to manage my controls from within the view model and that code-behind free files are mandatory to MVVM. But no problem.
– MacPowder
Jan 2 at 21:10
oh.. I didn't pay attention to the name... and no, that's not what I said at all... you were too busy going on your rant to read I guess lol... but yes, I'll stick to my statement about the 2nd part... if you are using code behind you aren't packaging stuff correctly. It's simply not needed and its poor design to split the logic. I'll also stick to my other point about your approach being bad since you are tightly coupling a bunch of things. But hey... you work at BMW, so what do I know lol...
– SledgeHammer
Jan 2 at 21:21
|
show 1 more comment
That wouldn't (or possibly wouldn't) work if you are using virtualization (which is the default) since containers get destroyed when scrolled out of view and you'll get a null exception. You'd have to scroll item 0 into view first, then wait for the container to be generated. Only then can you set focus to it.
– SledgeHammer
Jan 2 at 18:56
This time you are right. When virtualization is used we have to take care that the requested item container is generated before we can access it. This time your comment has some value. This is how we do it. That's professional behavior.
– MacPowder
Jan 2 at 21:00
"this time"? Do I know you? lol
– SledgeHammer
Jan 2 at 21:06
Yeah, feels like you are stalking me. I am that guy that you are trying to tell to manage my controls from within the view model and that code-behind free files are mandatory to MVVM. But no problem.
– MacPowder
Jan 2 at 21:10
oh.. I didn't pay attention to the name... and no, that's not what I said at all... you were too busy going on your rant to read I guess lol... but yes, I'll stick to my statement about the 2nd part... if you are using code behind you aren't packaging stuff correctly. It's simply not needed and its poor design to split the logic. I'll also stick to my other point about your approach being bad since you are tightly coupling a bunch of things. But hey... you work at BMW, so what do I know lol...
– SledgeHammer
Jan 2 at 21:21
That wouldn't (or possibly wouldn't) work if you are using virtualization (which is the default) since containers get destroyed when scrolled out of view and you'll get a null exception. You'd have to scroll item 0 into view first, then wait for the container to be generated. Only then can you set focus to it.
– SledgeHammer
Jan 2 at 18:56
That wouldn't (or possibly wouldn't) work if you are using virtualization (which is the default) since containers get destroyed when scrolled out of view and you'll get a null exception. You'd have to scroll item 0 into view first, then wait for the container to be generated. Only then can you set focus to it.
– SledgeHammer
Jan 2 at 18:56
This time you are right. When virtualization is used we have to take care that the requested item container is generated before we can access it. This time your comment has some value. This is how we do it. That's professional behavior.
– MacPowder
Jan 2 at 21:00
This time you are right. When virtualization is used we have to take care that the requested item container is generated before we can access it. This time your comment has some value. This is how we do it. That's professional behavior.
– MacPowder
Jan 2 at 21:00
"this time"? Do I know you? lol
– SledgeHammer
Jan 2 at 21:06
"this time"? Do I know you? lol
– SledgeHammer
Jan 2 at 21:06
Yeah, feels like you are stalking me. I am that guy that you are trying to tell to manage my controls from within the view model and that code-behind free files are mandatory to MVVM. But no problem.
– MacPowder
Jan 2 at 21:10
Yeah, feels like you are stalking me. I am that guy that you are trying to tell to manage my controls from within the view model and that code-behind free files are mandatory to MVVM. But no problem.
– MacPowder
Jan 2 at 21:10
oh.. I didn't pay attention to the name... and no, that's not what I said at all... you were too busy going on your rant to read I guess lol... but yes, I'll stick to my statement about the 2nd part... if you are using code behind you aren't packaging stuff correctly. It's simply not needed and its poor design to split the logic. I'll also stick to my other point about your approach being bad since you are tightly coupling a bunch of things. But hey... you work at BMW, so what do I know lol...
– SledgeHammer
Jan 2 at 21:21
oh.. I didn't pay attention to the name... and no, that's not what I said at all... you were too busy going on your rant to read I guess lol... but yes, I'll stick to my statement about the 2nd part... if you are using code behind you aren't packaging stuff correctly. It's simply not needed and its poor design to split the logic. I'll also stick to my other point about your approach being bad since you are tightly coupling a bunch of things. But hey... you work at BMW, so what do I know lol...
– SledgeHammer
Jan 2 at 21:21
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001793%2fwpf-how-to-auto-scroll-to-my-first-listviewitem-when-form-load%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you tried using Focus Manager for this? Also if the ListView is bound to a ViewModel then you could control if there is anything selected without handling the code behind or relying on puny loaded events.
– XAMlMAX
Jan 2 at 9:10
Can i have simple code example of what you talking ?
– user979033
Jan 2 at 11:35
Sure.
<ListView ItemsSource="{Binding Clipboards}" SelectedItem="{Binding SelectedClipboard}" Name="lst" FocusManager.FocusedElement="{Binding ElementName=lst}"/>
.– XAMlMAX
Jan 2 at 12:49