WPF列表不刷新

时间:2018-02-07 07:44:06

标签: c# wpf windows xaml

我正在尝试在WPF应用程序中创建新页面。这是一个“旧”项目,发现项目和语言(我知道C / C ++,而不是C#和XAML)。在那个页面中,我必须列出一个“关系”列表。但是我不能用新项目刷新该列表,也不能刷新项目。

我无法将该列表放在XAML中,我必须使用C#变量。我尝试了很多东西,比如List,可观察列表,刷新,绑定,扩展器中的触发事件,所以很多代码可能无用/无法访问。

对于精确度,piramidHandler.GetListParamReport();必须属于piramid类。

非常感谢你的帮助,请原谅我的英语,但仍然不是我的母语,但我尽力做到最好。

Rapports.xaml:

<Expander Header="{x:Static resx:StringResources.Lib_TabRapports}" VerticalAlignment="Top" Grid.Row="2" IsExpanded="False" IsEnabled="True" Expanded="ExpanderRapport">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <!--First row for controls-->
                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="650" />
                        <ColumnDefinition Width="650" />
                    </Grid.ColumnDefinitions>
                </Grid>
                <DataGrid ItemsSource="{Binding ListRapports}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserResizeColumns="False" Name="RapportGrid"
                  CanUserReorderColumns="False" Style="{StaticResource AzureDataGrid}" Grid.Row="1" FrozenColumnCount="2"
                  ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Auto">
                    <DataGrid.Resources>
                        <Style x:Key="AlignBottomColumnHeader" BasedOn="{StaticResource AzureDataGridColumnHeader}" TargetType="DataGridColumnHeader">
                            <Setter Property="VerticalContentAlignment" Value="Bottom" />
                        </Style>
                        <Style x:Key="RotatedColumnHeader" BasedOn="{StaticResource AzureDataGridColumnHeader}" TargetType="DataGridColumnHeader">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                            <Setter Property="VerticalContentAlignment" Value="Stretch" />
                            <Setter Property="Margin" Value="-4,0,0,0" />
                            <Setter Property="BorderBrush" Value="White" />
                            <Setter Property="LayoutTransform">
                                <Setter.Value>
                                    <RotateTransform Angle="-90" />
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGrid.Resources>

RapportViewModel.cs:

    public sealed partial class Piramid
        {
            internal ObservableCollection<ViewModels.Rapport> listRapports;

            public string GetRapportsList()
            {
                return piramidHandler.GetListParamReport();
            }

            public ObservableCollection<ViewModels.Rapport> ReloadRapportsLists()
            {
                var uniqueListRapport = new HashSet<ViewModels.Rapport>();
                string gabaritsXML = GetRapportsList();

           // [...] Fill uniqueListRapport

                return new ObservableCollection<ViewModels.Rapport>(uniqueListRapport.ToList());
            }
    }
public class TabRapport : ViewModelBase
    {
        #region Properties
        public TabRapport()
        {

        }


        public ObservableCollection<ViewModels.Rapport> ListRapports
        {
            get
            {
                ObservableCollection<Rapport> test;
                test = Piramid.Instance.ReloadRapportsLists();
                if (test.Count == 0)
                {
                    ViewModels.Rapport current = new ViewModels.Rapport
                    {
                        IdRapport = 52,
                        NameRapport = "This is a try"
                    };
                    test.Add(current);
                    ViewModels.Rapport current2 = new ViewModels.Rapport
                    {
                        IdRapport = 54,
                        NameRapport = "Second try"
                    };
                    test.Add(current2);
                }
                return test;
            }
        }

        #endregion Properties

        #region commands

// Useless in the context

        #endregion commands
    }

    public class Rapport : ViewModelBase
    {
        public Rapport()
        {
            idRapport = -1;
        }

        public Rapport(Rapport e)
        {
            idRapport = e.IdRapport;
            nameRapport = e.NameRapport;
        }

        private int idRapport;
        private string nameRapport;

        public int IdRapport
        {
            get => idRapport;
            set => idRapport = value;
        }

        public string NameRapport
        {
            get => nameRapport;
            set => nameRapport = value;
        }
    }

Rapports.xaml.cs:

public partial class Rapports : UserControl, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            OnTargetUpdated();
    }

    private void ExpanderRapport(object sender, RoutedEventArgs args)
    {
        OnTargetUpdated();
    }

    public Rapports()
    {
        InitializeComponent();
        Thread.CurrentThread.CurrentCulture = App.AppCultureInfo;
        Thread.CurrentThread.CurrentUICulture = App.AppCultureInfo;
    }

    public void OnTargetUpdated()
    {
        RapportsRoot.Reload();
        RapportGrid.Items.Refresh();
    }

    public void OnTargetUpdated(object sender, System.EventArgs e)
    {
        RapportsRoot.Reload();
        RapportGrid.Items.Refresh();
    }
}

编辑:

DataContext:DataContext="{Binding Source={StaticResource Locator}, Path=OngletRapport}">

参考:

public TabRapport OngletRapport
        {
            get
            {
                return ServiceLocator.Current.GetInstance<TabRapport>();
            }
        }

编辑2:在RapportViewModel.cs中添加了代码。我当前的尝试只是显示“这是一个尝试”和“第二次尝试”作为列表,在listRapport中设置数据后无法更改它

1 个答案:

答案 0 :(得分:0)

好的,我终于找到了答案。

我需要在所有后台traitement结束时使用ObservableObject.RaisePropertyChanged("ListRapports");,强制listRapports刷新所有数据和项目编号。

非常感谢您的帮助:)