2011-10-03 76 views
0

我有一個ListView中ListView的項目有按鈕和文本塊....的ListViewItem不選擇上按鈕單擊

塞納里奧:

我可以點一下出選擇ListView項即是選擇最後一個項目,然後如果我嘗試點擊第一個項目的按鈕,第一次沒有被選中(在DataGrid中它選擇)。

我無法使用DataGrid,因爲我在ListView中使用CustomView。

如果你需要我的代碼的問題,我會後的參考..

在這方面的任何幫助將用於CustomViews很大

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}" /> 

我的DataTemplates:

<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> 
+0

請分享一個很好的代碼片段來展示您的實現/問題。 –

+0

當然生病吧 – Ankesh

+0

@mzabsky感謝gr8,但是在編輯時別忘了給這個標題一些愛。 – Will

回答

3

與大多數情況一樣,有很多方法可以做到這一點。這裏有一個我剛剛在一分鐘內扔在一起......

考慮以下模型:

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

我想顯示它們的集合,用戶選擇通過一個按鈕一個。這意味着我需要在我的ViewModel三件事情:

  1. ItemModels
  2. A「的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 
     }; 
    } 
} 

最後,我將我的UI與基本的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實現,因爲它很簡單。

+0

它意味着在ICommand實現中會通過CommandParameter對象設置「SelectedItem」屬性。你也告訴你使用DependencyObject插入INPC是否有優勢使用MVVM – Ankesh

+0

Opps你已經在命令初始化程序中做到了這一點...我的錯誤 – Ankesh

+0

@ adcool2007:DP是WPF中最快的綁定,我發現它們很多比INPC更容易處理。它是個人的事情。 – Will