ListViewItem не выбирается при нажатии кнопки - PullRequest
0 голосов
/ 03 октября 2011

У меня есть ListView, в котором элементы ListView имеют кнопку и текстовый блок ....

Senario:

Я могу нажать кнопку без выбора элемента ListView, т. Е. Выбрать последний элемент, а затем, если я пытаюсь нажать кнопку первого элемента, первый раз не выбирается (в DataGrid он выбирается).

Я не могу использовать DataGrid, поскольку использую CustomView в ListView.

Если вам нужен мой код для решения проблемы, я опубликую его.

Любая помощь в этом отношении была бы велика

My ListView :  

  <ListView Name="lv"
              Grid.Row="1"
              DisplayMemberPath="Name"
              IsTextSearchEnabled="True"
              ItemsSource="{Binding}"
              KeyboardNavigation.DirectionalNavigation="Cycle"
              SelectionMode="Single"
              TextSearch.TextPath="{Binding Path=Person.Name}"
              View="{Binding Path=SelectedItem,
                             ElementName=viewComboBox}" />

Мои шаблоны данных для пользовательских представлений:

 <Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type CustomView:PlainView},
                                    ResourceId=ImageView}"
       BasedOn="{StaticResource {x:Type ListBox}}"
       TargetType="{x:Type ListView}">
    <Setter Property="BorderBrush" Value="Black" />
    <Setter Property="BorderThickness" Value=".5" />
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="ItemContainerStyle" Value="{Binding (ListView.View).ItemContainerStyle, RelativeSource={RelativeSource Self}}" />
    <Setter Property="ItemTemplate" Value="{Binding (ListView.View).ItemTemplate, RelativeSource={RelativeSource Self}}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Border Name="bd"
                        Margin="{TemplateBinding Margin}"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <ScrollViewer Margin="{TemplateBinding Padding}">
                        <WrapPanel KeyboardNavigation.DirectionalNavigation="Cycle" 
                                   Width="{Binding ActualWidth,
                                                   RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
                                   MinWidth="{Binding (ListView.View).MinWidth,
                                                      RelativeSource={RelativeSource Mode=FindAncestor,
                                                                                     AncestorType={x:Type ListView}}}"
                                   IsItemsHost="True"
                                   ItemWidth="{Binding (ListView.View).ItemWidth,
                                                       RelativeSource={RelativeSource Mode=FindAncestor,
                                                                                      AncestorType={x:Type ListView}}}" Orientation="Vertical"
                                   Height="{Binding ActualHeight,
                                                   RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type CustomView:PlainView},
                                    ResourceId=ImageViewItem}"
       BasedOn="{StaticResource {x:Type ListBoxItem}}"
       TargetType="{x:Type ListViewItem}">
    <Setter Property="Padding" Value="3" />
    <Setter Property="Margin" Value="5" />
    <Setter Property="BorderBrush" Value="Black" />
    <Setter Property="BorderThickness" Value="2" />
    <Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>

<DataTemplate x:Key="centralTile">
    <StackPanel Width="80" Height="40" KeyboardNavigation.AcceptsReturn="True">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="30"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button x:Name="tempabc" Command="{Binding Path=Launch}" KeyboardNavigation.AcceptsReturn="True" >
                <TextBlock Text="{Binding Path=Name}" FocusManager.IsFocusScope="True"></TextBlock>
            </Button>
            <Image Grid.Column="1" Source="Water lilies.jpg"/>
        </Grid>
        <TextBlock
                           HorizontalAlignment="Center"
                           FontSize="13"
                           Text="{Binding Path=Name}" />
    </StackPanel>
</DataTemplate>
<CustomView:PlainView x:Key="plainView"
                              ItemTemplate="{StaticResource ResourceKey=centralTile}"
                              ItemWidth="100" />  
<GridView x:Key="myGridView">
        <GridViewColumn>
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <Button>
                        <TextBlock Text="{Binding Path=Name}" />
                    </Button>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>

1 Ответ

3 голосов
/ 03 октября 2011

Как и в большинстве случаев, есть несколько способов сделать это. Вот тот, который я только что выбросил за минуту ...

Учитывая следующую модель:

public sealed class ItemModel
{
    public string Name { get; set; }
}

Я хочу показать коллекцию из них пользователю и выбрать один с помощью кнопки. Это означает, что мне нужно три вещи в моей ViewModel:

  1. Коллекция ItemModels
  2. Свойство «SelectedItem» для хранения текущего выбранного экземпляра
  3. Реализация ICommand для привязки к кнопкам в представлении

Я создаю свою ViewModel и добавляю в нее эти элементы. Обратите внимание, что я предпочитаю, чтобы мои ViewModels расширяли DependencyObject, а не связывались с INPC.

public sealed class ViewModel : DependencyObject
{
    // 1. A collection of ItemModels
    public ObservableCollection<ItemModel> ItemModels { get; private set; }
    // 2. A "SelectedItem" property to hold the currently selected instance
    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register(
            "SelectedItem",
            typeof(ItemModel),
            typeof(ViewModel),
            new UIPropertyMetadata(null));
    public ItemModel SelectedItem
    {
        get { return (ItemModel)GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }
    // 3. An ICommand implementation to bind to the buttons in the View
    public Command SelectItem { get; private set; }
    public ViewModel()
    {
        ItemModels = new ObservableCollection<ItemModel>();
        ItemModels.Add(new ItemModel { Name = "One" });
        ItemModels.Add(new ItemModel { Name = "Two" });
        ItemModels.Add(new ItemModel { Name = "Three" });
        SelectItem = new Command 
        { 
            ExecuteAction = x => SelectedItem = x as ItemModel 
        };
    }
}

Наконец, я соединяю свой пользовательский интерфейс с элементарным ListView.

<Window
    x:Class="q_7635202.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="WindowRoot">
    <ListView
        SelectedItem="{Binding SelectedItem}"
        ItemsSource="{Binding ItemModels}">
        <ListView.View>
            <GridView>
                <GridViewColumn
                    DisplayMemberBinding="{Binding Name}"
                    Header="Name" /> 
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button
                                Content="Select"
                                Command="{Binding DataContext.SelectItem,
                                                  ElementName=WindowRoot}"
                                CommandParameter="{Binding}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Window>

Все довольно просто. Я опускаю реализацию ICommand, поскольку она тривиальна.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...