2009-11-18 74 views
4

我有一些XAML像這樣(的Silverlight 3.0),選擇的項目的DataGrid

<data:DataGrid Margin="0,20,0,0" ItemsSource="{Binding Path=Upgrades}" = 
    AutoGenerateColumns="False" VerticalAlignment="Center" 
    AlternatingRowBackground="AliceBlue" HorizontalAlignment="Left" 
    SelectionMode="Single"> 
    <data:DataGrid.RowStyle> 
     <Style TargetType="data:DataGridRow"> 
      <Setter Property="IsSelected" Value="{Binding Path=IsSelected}"/> 
     </Style> 
    </data:DataGrid.RowStyle> 
    <data:DataGrid.Columns> 
     <data:DataGridTextColumn Header="Product Version" Binding="{Binding Path=ProductVersion}"/> 
     <data:DataGridTextColumn Header="Upgrade Version" Binding="{Binding Path=UpgradeVersion}"/> 
     <data:DataGridTextColumn Header="Description" Binding="{Binding Path=UpgradeDescription}" Width="350"/> 
     <data:DataGridTextColumn Header="Database Lock Required" Binding="{Binding Path=DatabaseLockRequired}"></data:DataGridTextColumn> 
    </data:DataGrid.Columns> 
</data:DataGrid> 

所以基本上我有升級視圖模型類的可觀察的名單是叫物業「升級」你可以看到有。每個升級視圖模型類都具有綁定到的屬性(例如ProductVersion等)。

我還添加了一個名爲IsSelected的屬性。我想綁定該屬性,以便在網格視圖中選擇一個項目時自動設置。在DataGridRow類中似乎沒有名爲IsSelected的屬性。

我想上面的例子在使用ListView時可以在WPF中工作。

回答

2

嘗試添加該列到DataGrid

<data:DataGridCheckBoxColumn Header="IsSelected" Binding="{Binding Path=IsSelected, Mode=TwoWay}"></data:DataGridCheckBoxColumn> 

然後一個名爲IsSelected屬性添加到您的viewmodel

 private bool _IsSelected = true; 
     public bool IsSelected    
     { 
      get 
      { 
       return _IsSelected; 
      } 
      set 
      { 
       if (value != _IsSelected) 
       { 
        _IsSelected = value; 
        OnPropertyChanged("IsSelected"); 
       } 
      } 

現在當您選擇數據網格中的複選框時,它將在您的ViewModel中被選中

0

嘗試此處結合selectedItem屬性的電網是一個

例子:

   <my:DataGrid Width="Auto" 
        SelectedItem="{Binding Path=AcronymsDefinitions_SelectedItem}" 
        ItemsSource="{Binding Path=DataGridSource}" 
        VerticalContentAlignment="Stretch" 
        HorizontalContentAlignment="Stretch" 
        VerticalAlignment="Stretch" 
        HorizontalAlignment="Stretch" 
        MaxColumnWidth="500" 
        Margin="0,0,0,0" 
        Background="#FFC8C8C8" 
        Foreground="#FF333333" 
        VerticalGridLinesBrush="AntiqueWhite" 
        HorizontalGridLinesBrush="AliceBlue" />