簡體   English   中英

嵌套的DataGrid將值綁定到屬性

[英]Nested DataGrid Binding Value to Property

我有一個綁定到名為HldList的ListDataGrid ,該List的類型為Holding (類在下面顯示為Fund )。

當選擇了其中一行時,行詳細信息將展開以顯示另一個DataGrid (我們稱其為我的子數據網格),這綁定到了List Funds。

綁定均按預期工作。

行詳細信息模板的代碼在我的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));
          }
 }    

應用程式

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM