嵌套的数据网格绑定值到属性
本文关键字:属性 绑定 网格 数据 数据网 嵌套 | 更新日期: 2023-09-27 18:31:54
我有一个绑定到一个名为 HldList 的List
的DataGrid
,该的类型为 Holding
(类在下面显示为 Fund
)。
当选择其中一行时,行详细信息将展开以显示另一个DataGrid
(我们称之为我的子数据网格),这绑定到List
资金。
绑定都按预期工作。
我的行详细信息模板的代码在我的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));
}
}
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}}}