嵌套DataGrid绑定到属性的值

时间:2016-01-26 16:13:09

标签: c# .net wpf xaml

我有DataGrid绑定到List名为HldList的Holding类型Fund(类在下面显示为DataGrid)。

当选择其中一行时,行详细信息将展开以显示另一个List(让我们称之为我的子数据网格),这将绑定到app.xaml基金。

绑定全部按预期工作。

我的行详情模板的代码位于我的DataGrid文件中,该文件显示在此帖的底部。

我能做的一件事就是以下几点。在我的行详细信息TextBox中,我还有一个TextBox,用户可以在其中输入值(在这种情况下是一个比率)我想将此 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)); } } 的值绑定到定义的Ratio属性在我的控股课上,但似乎无法让它工作

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

的App.xaml

bundle update

1 个答案:

答案 0 :(得分:2)

问题是HldList不在DataContext中。稍高于你使用

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

在调用HldList时使用相对源应解决您的问题

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