Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 嵌套的DataGrid将值绑定到属性_C#_.net_Wpf_Xaml - Fatal编程技术网

C# 嵌套的DataGrid将值绑定到属性

C# 嵌套的DataGrid将值绑定到属性,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我有一个数据网格,它绑定到一个名为HldList的列表,该列表属于控股(下面显示的类为基金) 当选择其中一行时,行详细信息将展开以显示另一个DataGrid(让我们将其称为我的子DataGrid)这将绑定到列表资金 所有绑定都按预期工作 我的行详细信息模板的代码位于我的app.xaml文件中,该文件显示在本文底部 有一件事我不能去工作,那就是以下几点。在我的行详细信息DataGrid中,我还有一个TextBox,用户可以在其中输入一个值(在本例中是一个比率),我想将这个TextBox的值绑定到我

我有一个
数据网格
,它绑定到一个名为HldList的
列表
,该列表属于
控股
(下面显示的类为
基金

当选择其中一行时,行详细信息将展开以显示另一个
DataGrid
(让我们将其称为我的子DataGrid)这将绑定到
列表
资金

所有绑定都按预期工作

我的行详细信息模板的代码位于我的
app.xaml
文件中,该文件显示在本文底部

有一件事我不能去工作,那就是以下几点。在我的行详细信息
DataGrid
中,我还有一个
TextBox
,用户可以在其中输入一个值(在本例中是一个比率),我想将这个
TextBox
的值绑定到我的Holding类中定义的ratio属性,但似乎无法让它工作

课程

 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));
          }
 }    
类保持:INotifyPropertyChanged
{
私有字符串_码;
公共字符串代码
{
得到
{
返回代码;
}
设置
{
_代码=值;
OnPropertyChanged(“代码”);
}
}
私有字符串_比率;
公共字符串比率
{
得到
{
回报率;
}
设置
{
_比率=数值;
不动产变动(“比率”);
}
}
私人上市基金;
上市基金
{
得到
{
返还资金;
}
设置
{
_资金=价值;
不动产变更(“基金”);
}
}
公共事件属性更改事件处理程序属性更改;
void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
类别资金:INotifyPropertyChanged
{
私有字符串\u名称;
公共字符串名
{
得到
{
返回_name;
}
设置
{
_名称=值;
不动产变更(“名称”);
}
}
私人双(名义),;
公共双名义
{
得到
{
名义回报率;
}
设置
{
_标称值=额定值;
不动产变更(“名义”);
}
}
公共事件属性更改事件处理程序属性更改;
void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的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>

问题在于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}}}

问题是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}}}

我试过了,但还是不走运。我做了以下操作,它不应该是SelectedItem.Ratio而不是HldList.Ratio吗?是的,就是这样!谢谢,我试过了,但还是不走运。我做了以下操作,它不应该是SelectedItem.Ratio而不是HldList.Ratio吗?是的,就是这样!谢谢