mHelpMe
mHelpMe

Reputation: 6668

Nested DataGrid Binding Value to Property

I have a DataGrid that is bound to a List called HldList which is of type Holding (class is shown below as Fund).

When one of the rows is selected the row details expands to show another DataGrid (lets call this my sub datagrid) this is bound to the List Funds.

The bindings all work as expected.

The code for my row details template is in my app.xaml file which is shown at the bottom of this post.

The one thing I can't get to work is the following. In my row details DataGrid I also have a TextBox where a user can enter a value (a ratio in this case) I want to bind the value of this TextBox to the Ratio property defined in my Holding class but can't seem to get it to work

Classes

 class Holding : INotifyPropertyChanged
 {
        private string _code;
        public string Code
        {
            get
            {
                return _code;
            }
            set
            {
                _code = value;
                OnPropertyChanged("Code");
            }
        }

        private string _ratio;
        public string Ratio
        {
            get
            {
                return _ratio;
            }
            set
            {
                _ratio = value;
                OnPropertyChanged("Ratio");
            }
        }

        private List<Fund> _funds;
        public List<Fund> Funds
        {
            get
            {
                return _funds;
            }
            set
            {
                _funds = value;
                OnPropertyChanged("Funds");
            }
        }
          public event PropertyChangedEventHandler PropertyChanged;

          void OnPropertyChanged(string propertyName)
          {
              if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
 }


 class Funds : INotifyPropertyChanged
 {
        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        private double _nominal;
        public double Nominal
        {
            get
            {
                return _nominal;
            }
            set
            {
                _nominal = value;
                OnPropertyChanged("Nominal");
            }
        }
          public event PropertyChangedEventHandler PropertyChanged;

          void OnPropertyChanged(string propertyName)
          {
              if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
 }    

App.xaml

 <DataTemplate x:Key="DG_RowDetailRatio">
        <Grid x:Name="RowDetailGrid"            
              Margin="5"
              HorizontalAlignment="Left">
            <Border HorizontalAlignment="Left"
                    VerticalAlignment="Top"
                    Height="250"
                    CornerRadius="5">
                <Border.Background>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                        <GradientStop Offset="0" Color="Transparent"/>
                        <GradientStop Offset="1" Color="Transparent"/>
                    </LinearGradientBrush>
                </Border.Background>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="4*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="400"/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0"
                               Grid.Column="0"
                               Margin="5,5,5,5"
                               HorizontalAlignment="Left"
                               FontSize="12"
                               FontWeight="Bold"
                               Foreground="Black" 
                               Text="Select funds to be updated">
                    </TextBlock>
                    <DataGrid Grid.Row="1" Grid.Column="0"  Grid.ColumnSpan="2"
                              ItemsSource="{Binding SelectedItem.Funds,  RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"                                                                  
                              RowStyle="{StaticResource DG_Row}"  
                              ColumnHeaderStyle="{StaticResource DG_ColumnHeader}" 
                              RowHeaderStyle="{StaticResource DG_RowHeaderNested}"
                              CellStyle="{StaticResource DG_Cell}" 
                              Background="Silver"
                              HorizontalGridLinesBrush="LightGray"
                              VerticalGridLinesBrush="LightGray"
                              CanUserAddRows="False"
                              CanUserDeleteRows="False"
                              Margin="50,5,5,20"
                              AutoGenerateColumns="False">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True" MinWidth="75"/>
                            <DataGridTextColumn Header="Nominal" Binding="{Binding Nominal}" IsReadOnly="True" MinWidth="75"/>
                        </DataGrid.Columns>
                    </DataGrid>
                    <Grid Grid.Row="1" Grid.Column="2">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Row="0" Grid.Column="0" Margin="50,5,0,0" HorizontalAlignment="Left" FontSize="12"
                               FontWeight="Bold" Foreground="Black" Text="Or enter Ratio against acquired company nominals">
                        </TextBlock>
                        <CheckBox x:Name="chkRatio" Grid.Row="0" Grid.Column="1" Margin="20,5,0,0" Height="30" 
                                  IsChecked="{Binding UseRatio}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                        <TextBox Grid.Row="0" Grid.Column="2" Height="30" Width="50"  Margin="20,5,0,0" 
                                 ToolTip="Enter ratio" HorizontalAlignment="Left" VerticalAlignment="Top"
                                 Text="{Binding HldList.Ratio, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                 Visibility="{Binding IsChecked, ElementName=chkRatio, Converter={StaticResource BoolToVis}}"/>
                    </Grid>
                </Grid>
            </Border>
        </Grid>
    </DataTemplate>

Upvotes: 0

Views: 174

Answers (1)

Geoffrey
Geoffrey

Reputation: 976

The problem is that HldList is not in the DataContext. A bit above you use

ItemsSource="{Binding SelectedItem.Funds,  
            RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" 

Using a relative source when calling HldList should fix your problem

Text="{Binding SelectedItem.Ratio, UpdateSourceTrigger=PropertyChanged,
     Mode=TwoWay}", 
     RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}

Upvotes: 2

Related Questions