如何在更改另一列时刷新 WPF 数据网格中的依赖列?
How to refresh dependent column in WPF datagrid when another column is changed?
所以我得到了一个这样创建的 WPF 数据网格:
<DataGrid x:Name="columns" Grid.Row="1" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" ItemsSource="{Binding}" DataContext="{StaticResource ColumnsCollection}"
AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" AddingNewItem="columns_AddingNewItem">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="Auto"/>
<DataGridTextColumn Header="Width of Name" Binding="{Binding WidthOfNameInCentimeter}" Width="Auto"/>
</DataGrid.Columns>
</DataGrid>
初始化如下:
CollectionViewSource itemCollectionViewSource;
itemCollectionViewSource = (CollectionViewSource)(FindResource("ColumnsCollection"));
itemCollectionViewSource.Source = new ObservableCollection<FormCreatorColumnInfoDatasource>();
并填充。
FormCreatorColumnInfoDatasource 如下所示:
class FormCreatorColumnInfoDatasource : INotifyPropertyChanged
{
private IFormCreatorColumnInfo m_info;
public string Name
{
get
{
return m_info.Name;
}
set
{
m_info.Name = value;
}
}
public string WidthOfNameInCentimeter
{
get
{
var exactWidthInCentimeter = Name.Length * 0.238M;
var roundedUpToHalfCentimeter = Math.Round(exactWidthInCentimeter * 2, MidpointRounding.AwayFromZero) / 2;
return (roundedUpToHalfCentimeter).ToString();
}
}
}
所以现在我想在用户手动更改列的名称时立即更改列 WidthOfNameInCentimeter(名称宽度)的内容,以便此列中始终有正确的信息。
我该怎么做?
在@lidqy 的帮助下,我添加了一个函数并更改了 Name:
的 setter
class FormCreatorColumnInfoDatasource : INotifyPropertyChanged
{
private IFormCreatorColumnInfo m_info;
public string Name
{
get
{
return m_info.Name;
}
set
{
m_info.Name = value;
OnPropertyChanged(nameof(Name));
OnPropertyChanged(nameof(WidthOfNameInCentimeter));
}
}
public string WidthOfNameInCentimeter
{
get
{
var exactWidthInCentimeter = Name.Length * 0.238M;
var roundedUpToHalfCentimeter = Math.Round(exactWidthInCentimeter * 2, MidpointRounding.AwayFromZero) / 2;
return (roundedUpToHalfCentimeter).ToString();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
不要忘记在数据源中实现 INotifyPropertyChanged 并将 ObservableCollection 用于数据网格源。
所以我得到了一个这样创建的 WPF 数据网格:
<DataGrid x:Name="columns" Grid.Row="1" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" ItemsSource="{Binding}" DataContext="{StaticResource ColumnsCollection}"
AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" AddingNewItem="columns_AddingNewItem">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="Auto"/>
<DataGridTextColumn Header="Width of Name" Binding="{Binding WidthOfNameInCentimeter}" Width="Auto"/>
</DataGrid.Columns>
</DataGrid>
初始化如下:
CollectionViewSource itemCollectionViewSource;
itemCollectionViewSource = (CollectionViewSource)(FindResource("ColumnsCollection"));
itemCollectionViewSource.Source = new ObservableCollection<FormCreatorColumnInfoDatasource>();
并填充。
FormCreatorColumnInfoDatasource 如下所示:
class FormCreatorColumnInfoDatasource : INotifyPropertyChanged
{
private IFormCreatorColumnInfo m_info;
public string Name
{
get
{
return m_info.Name;
}
set
{
m_info.Name = value;
}
}
public string WidthOfNameInCentimeter
{
get
{
var exactWidthInCentimeter = Name.Length * 0.238M;
var roundedUpToHalfCentimeter = Math.Round(exactWidthInCentimeter * 2, MidpointRounding.AwayFromZero) / 2;
return (roundedUpToHalfCentimeter).ToString();
}
}
}
所以现在我想在用户手动更改列的名称时立即更改列 WidthOfNameInCentimeter(名称宽度)的内容,以便此列中始终有正确的信息。
我该怎么做?
在@lidqy 的帮助下,我添加了一个函数并更改了 Name:
的 setterclass FormCreatorColumnInfoDatasource : INotifyPropertyChanged
{
private IFormCreatorColumnInfo m_info;
public string Name
{
get
{
return m_info.Name;
}
set
{
m_info.Name = value;
OnPropertyChanged(nameof(Name));
OnPropertyChanged(nameof(WidthOfNameInCentimeter));
}
}
public string WidthOfNameInCentimeter
{
get
{
var exactWidthInCentimeter = Name.Length * 0.238M;
var roundedUpToHalfCentimeter = Math.Round(exactWidthInCentimeter * 2, MidpointRounding.AwayFromZero) / 2;
return (roundedUpToHalfCentimeter).ToString();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
不要忘记在数据源中实现 INotifyPropertyChanged 并将 ObservableCollection 用于数据网格源。