0

I have a simple dialog with MVVM, just a ListView and two Buttons (Ok, Cancel) and a ViewModel responsible for binding the ItemsSource and SelectedItem of the ListView.

View:

...
<StackPanel>
    <ScrollViewer VerticalScrollBarVisibility="Auto">
        <ListView ItemsSource="{Binding Projects}" SelectedItem="{Binding Project}" SelectionMode="Single">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ScrollViewer>
    <StackPanel Orientation="Horizontal" FlowDirection="RightToLeft">
        <Button Content="Cancel" IsCancel="True"/>
        <Button Content="Ok" Command="{Binding OkCommand}" CommandParameter="{Binding ElementName=Dlg}"/>
    </StackPanel>
</StackPanel>
...

Viewmodel:

public class SelectProjectViewModel : ViewModelBase, IModalDialogViewModel
{
    private readonly ProjectList _projectList;
    public ProjectList Projects => _projectList;

    public Project Project
    {
        get { return GetValue(() => Project); }
        set { SetValue(() => Project, value);}
    }

    private RelayCommand _okCommand;
    public RelayCommand OkCommand => _okCommand ?? (_okCommand = new RelayCommand(OkCommandCall));

    public SelectProjectViewModel(ProjectList projectList, Project currentProject)
    {
        _projectList = projectList;
        this.Project = currentProject;
    }

    private void OkCommandCall(object window)
    {
        DialogResult = true;
        WindowCloseBehaviour.SetClose(window as DependencyObject, true);
    }

    public bool? DialogResult
    {
        get { return GetValue(() => DialogResult); }
        private set { SetValue(() => DialogResult, value); }
    }
}

I tried to achieve focusing the first Element like described here, with the accepted solution and the equivalent behaviour class. But this always sets the focus on the Cancel-Button, while I want my ListView to be focused when opening the dialog, so that the selected element is really highlighted, not just in the "inactive highlight" color. And no, I don't want to workaround with changing the colors.

Community
  • 1
  • 1
Florian Koch
  • 1,372
  • 1
  • 30
  • 49

1 Answers1

0

Using simple FocusedElement on a parent with SelectedIndex on ListView itself works for me:

<StackPanel FocusManager.FocusedElement="{Binding ElementName=myListView}">
    <ScrollViewer VerticalScrollBarVisibility="Auto">
        <ListView Name="myListView" ItemsSource="{Binding Projects}" SelectedItem="{Binding Project}" SelectionMode="Single"
                  SelectedIndex="0">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ScrollViewer>
    ...
    </StackPanel>
</StackPanel>
icebat
  • 4,696
  • 4
  • 22
  • 36
  • @FlorianKoch, I emulated your example, and binding works correctly for me. Selected item binds to one set in code and all. How does it break? – icebat Feb 15 '16 at 13:04
  • I have one preselected listviewitem, and when I use your code the selection when first opening the dialog is gone – Florian Koch Feb 15 '16 at 13:09