views:

691

answers:

2

Hi All,

I have some XAML like this (silverlight 3.0),

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

So basically I have an observable list of upgrade view model classes which is the property called 'Upgrades' you can see there. Each upgrade view model class has the properties there that are being bound to (e.g. ProductVersion etc).

I also added a property called IsSelected. I wanted to bind that property so that when you select an item in the grid view it automatically gets set. There doesn't appear to be a property called IsSelected on the DataGridRow class.

I think the example above would work in WPF when using a ListView.

A: 

Try binding to the SelectedItem Property on the Grid here's an

Example:

                <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" />
vsmike
A: 

try adding this column to the datagrid

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

Then add a property called IsSelected to your viewmodel

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

Now when you select a checkbox in the datagrid, it will be selected in your ViewModel

Neil