2017-07-05 29 views
0

我將我的列表視圖綁定到使用Caliburn的視圖模型。我的觀點代碼如下:WPF添加更多的交互到列表視圖項目

<ListView x:Name="ListView" Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding ProjectsPMod}" Margin="110,0,110,131" HorizontalContentAlignment="Stretch" BorderThickness="0" Height="111" VerticalAlignment="Bottom"> 
     <interactivity:Interaction.Triggers> 
      <interactivity:EventTrigger EventName="SelectionChanged"> 
       <cal:ActionMessage MethodName="OpenProjectShell"> 
        <cal:Parameter Value="{Binding ElementName=ListView, Path=SelectedItem}" /> 
       </cal:ActionMessage> 
      </interactivity:EventTrigger> 
     </interactivity:Interaction.Triggers> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <StackPanel Orientation="Horizontal"> 
         <ContentControl Content="{StaticResource Appbar_Suitcase}" /> 
         <Label Content="{Binding Name}"/> 
        </StackPanel> 

        <Separator HorizontalAlignment="Stretch" Margin="0, 10, 0, 0"/> 
       </StackPanel> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

現在,每一行中的每個項目只顯示其名稱和公文包圖標。如果我想在該行下面添加一個「編輯」,如果您點擊了某處發生的事情(可能會彈出一個窗口),我該怎麼做?

+2

你會怎麼做?請縮小你的問題。 – mm8

+0

你可以改變'ItemTemplate'的複雜程度,想象Grids,ItemsControls,TabControls(請不要使用tab控件)。您可以輕鬆地將一個按鈕添加到模板,只需將其Command屬性綁定到視圖模型上的「ICommand」,即可使其運行該行爲。 –

+0

@ mm8使用綁定到Command的按鈕或映射到類似的超鏈接。 – Iason

回答

0

可以使一個普通Button看起來像使用模板的鏈接。只需添加一個ButtonStackPanel並將其綁定到一個命令:

<ListView x:Name="ListView" Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding ProjectsPMod}" Margin="110,0,110,131" HorizontalContentAlignment="Stretch" BorderThickness="0" Height="111" VerticalAlignment="Bottom"> 
    <interactivity:Interaction.Triggers> 
     <interactivity:EventTrigger EventName="SelectionChanged"> 
      <cal:ActionMessage MethodName="OpenProjectShell"> 
       <cal:Parameter Value="{Binding ElementName=ListView, Path=SelectedItem}" /> 
      </cal:ActionMessage> 
     </interactivity:EventTrigger> 
    </interactivity:Interaction.Triggers> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <StackPanel Orientation="Horizontal"> 
        <ContentControl Content="{StaticResource Appbar_Suitcase}" /> 
        <Label Content="{Binding Name}"/> 
       </StackPanel> 

       <Separator HorizontalAlignment="Stretch" Margin="0, 10, 0, 0"/> 

       <Button Margin="0 10 0 0" Content="Link" Cursor="Hand" Command="{Binding YourCommand}"> 
        <Button.Template> 
         <ControlTemplate TargetType="Button"> 
          <TextBlock TextDecorations="Underline"> 
           <ContentPresenter /> 
          </TextBlock> 
         </ControlTemplate> 
        </Button.Template> 
        <Button.Style> 
         <Style TargetType="Button"> 
          <Setter Property="Foreground" Value="Blue" /> 
          <Style.Triggers> 
           <Trigger Property="IsMouseOver" Value="True"> 
            <Setter Property="Foreground" Value="Red" /> 
           </Trigger> 
          </Style.Triggers> 
         </Style> 
        </Button.Style> 
       </Button> 
      </StackPanel> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

可以使用其Margin屬性來控制ButtonStackPanel中的位置。

+0

@Iason:請記住注意有用答案:https://stackoverflow.com/help/someone-answers – mm8

0

如果您的意思是「如何爲每一行添加編輯按鈕?」那麼你只需要在ItemTemplate中添加一個按鈕:

<ListView.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <StackPanel Orientation="Horizontal"> 
        <ContentControl Content="{StaticResource Appbar_Suitcase}" /> 
        <Label Content="{Binding Name}"/> 
        <!-- Edit button --> 
        <Button Command="{Binding EditCommandOnViewModel}" Content="Edit" /> 
       </StackPanel> 

       <Separator HorizontalAlignment="Stretch" Margin="0, 10, 0, 0"/> 
      </StackPanel> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
+0

這幾乎是我要找的。我們怎麼才能把那個按鈕放在那一行的下方,以免與名字如此接近?讓它看起來更像一個超鏈接而不是一個普通的按鈕? – Iason