2016-05-12 61 views
2

我正在使用wpf並使用ItemsControl來表示UI的類的列表。wpf中的項目控件mvvm

我的代碼是這樣的:

<ItemsControl ItemsSource="{Binding Path=UsecaseListItems}" > 

      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <WrapPanel> 
        <CheckBox IsChecked="{Binding Path=IsSelected,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,IsAsync=True}" 

           Margin="2,5"/> 
         <Button Content="{Binding UsecaseName}" 

           Margin="2,5"/> 
        </WrapPanel> 
       </DataTemplate> 

      </ItemsControl.ItemTemplate> 

     </ItemsControl> 

現在我的魔杖一個頭在列表的頂部添加到兩個列(全選和USENAME)。我該怎麼做??

+0

由於每個項目的模板都有它自己的包裝面板,我認爲談論「列」是一種錯誤陳述。特別是你的按鈕的內容是有約束力的。也許你應該考慮使用'ListView'而不是'ItemsControl'。 – Grx70

回答

0

下面是解決方案(不太好,但我認爲更糟糕的設計也是可以的),並請在您的代碼更改bindings

XAML:

<ItemsControl ItemsSource="{Binding Path=list}" > 
    <ItemsControl.Template> 
     <ControlTemplate> 
      <StackPanel Orientation="Vertical">     
       <Border BorderThickness="5" BorderBrush="DarkGray"> 
        <StackPanel Orientation="Horizontal"> 
         <Button Content="Select All" Width="80" HorizontalAlignment="Stretch"/> 
         <Button Content="User name" Width="80" HorizontalAlignment="Stretch"/> 
        </StackPanel> 
       </Border> 
       <Border BorderThickness="5" BorderBrush="LightGray"> 
        <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Path=ItemsSource}"> 

         <ItemsControl.ItemTemplate> 
          <DataTemplate> 
           <WrapPanel> 
            <CheckBox IsChecked="{Binding Path=DeviceState,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,IsAsync=True}" 
            HorizontalAlignment="Stretch" Width="100" Margin="2,5"/> 
            <Button Content="{Binding Name}" HorizontalAlignment="Stretch" Width="100" Margin="2,5"/> 
           </WrapPanel> 
          </DataTemplate> 

         </ItemsControl.ItemTemplate> 
        </ItemsControl> 
       </Border> 
      </StackPanel> 
     </ControlTemplate> 
    </ItemsControl.Template> 
</ItemsControl> 

輸出

list

爲什麼要按鈕作爲標題?你不能只把按鈕放在ItemsControl之外嗎?

-1

根據您的需要,最好使用ListView控件而不是ItemsControl

下面是示例。 。

<Grid> 
    <Grid.Resources> 
     <DataTemplate x:Key="checkboxHeaderTemplate"> 
      <CheckBox Checked="CheckBox_Checked"> //add command for MVVM 
      </CheckBox> 
     </DataTemplate> 

     <DataTemplate x:Key="CheckBoxCell"> 
      <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" >      
      </CheckBox> 
     </DataTemplate> 

     <DataTemplate x:Key="ButtonCell"> 
      <Button Content="{Binding Path=UsecaseName, Mode=TwoWay}" > 
      </Button> 
     </DataTemplate> 
    </Grid.Resources> 

    <ListView ItemsSource="{Binding Path=UsecaseListItems}" > 
     <ListView.View> 
      <GridView> 
       <GridView.Columns> 
        <GridViewColumn HeaderTemplate="{StaticResource checkboxHeaderTemplate}" 
             CellTemplate="{StaticResource CheckBoxCell}" Width="auto"> 
        </GridViewColumn> 

        <GridViewColumn HeaderTemplate="{StaticResource checkboxHeaderTemplate}" 
             CellTemplate="{StaticResource ButtonCell}" Width="auto"> 
        </GridViewColumn> 
       </GridView.Columns> 
      </GridView> 
     </ListView.View> 
    </ListView> 
</Grid> 

希望有幫助。

+0

@downvoter。 。考慮添加評論 – Gopichandar