简体   繁体   English

如何在更改另一列时刷新WPF数据网格中的依赖列?

[英]How to refresh dependent column in WPF datagrid when another column is changed?

So I got a WPF datagrid which is created like this:所以我得到了一个 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>

it is initialized like this:它是这样初始化的:

CollectionViewSource itemCollectionViewSource;
itemCollectionViewSource = (CollectionViewSource)(FindResource("ColumnsCollection"));
itemCollectionViewSource.Source = new ObservableCollection<FormCreatorColumnInfoDatasource>();

and populated.和人口稠密。

The FormCreatorColumnInfoDatasource looks like this: 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();
        }
    }       
    
}

So now I want to instantly change the content of the column WidthOfNameInCentimeter (Width of Name) when the user manually changes the Name of the column so there is always the correct information in this column.所以现在我想在用户手动更改列的名称时立即更改列 WidthOfNameInCentimeter(名称宽度)的内容,以便此列中始终有正确的信息。

How do I do this?我该怎么做呢?

With the help of @lidqy I added a function and changed the setter of Name:在@lidqy 的帮助下,我添加了一个 function 并更改了名称的设置器:

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

Dont forget to implement INotifyPropertyChanged in the datasource and to use an ObservableCollection for the datagrids source.不要忘记在数据源中实现 INotifyPropertyChanged 并为数据网格源使用 ObservableCollection。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM