Как отобразить выбранные значения в поле со списком после перезагрузки в wpf - PullRequest
0 голосов
/ 21 февраля 2020

Я пытаюсь отображать значения в моем ComboBox все время. Когда я запускаю страницу в первый раз, она отображается, но после перезагрузки она исчезает. Однако, когда я использую ComboBox, значения есть, но они не видны при выборе.

Это часть моего кода.

Пользовательский элемент управления:

xaml:

<ComboBox DataContext="{Binding RelativeSource={RelativeSource Self}}" Margin="2,0,2,0" Grid.Row="0" Width="60" Height="20" VerticalAlignment="Center"  HorizontalAlignment="Center" 
    ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=PagesList,UpdateSourceTrigger=PropertyChanged,  Mode=TwoWay,  NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True}"  
    SelectedItem="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=PageSelected, UpdateSourceTrigger=PropertyChanged,  Mode=TwoWay,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True }" 
                          />

<ComboBox DataContext="{Binding RelativeSource={RelativeSource Self}}" Margin="2,0,2,0"  Width="60" Height="20" VerticalAlignment="Center" HorizontalAlignment="Right" 
    ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=RecordsPerPageList, UpdateSourceTrigger=PropertyChanged,  Mode=TwoWay, NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True}"
    SelectedItem="{Binding RelativeSource={RelativeSource AncestorType=UserControl},  Path=RecordsPerPage, UpdateSourceTrigger=PropertyChanged,  Mode=TwoWay, NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True}"  
                      />

Это код C# для этого пользовательского элемента управления

public int RecordsPerPage
{
    get
    {
        this.DataContext = this;
        return (int)GetValue(RecordsPerPageProperty);
    }
    set
    {
        this.DataContext = this;
        SetValue(RecordsPerPageProperty, value);
    }
}
public static readonly DependencyProperty RecordsPerPageProperty = DependencyProperty.Register("RecordsPerPage", typeof(int), typeof(DataGridRecordsCounter), new PropertyMetadata(null));

public ObservableCollection<int> RecordsPerPageList
{
    get
    {
        this.DataContext = this;
        return (ObservableCollection<int>)GetValue(RecordsPerPageListProperty);
    }
    set
    {
        this.DataContext = this;
        SetValue(RecordsPerPageListProperty, value);
    }
}
public static readonly DependencyProperty RecordsPerPageListProperty = DependencyProperty.Register("RecordsPerPageList", typeof(ObservableCollection<int>), typeof(DataGridRecordsCounter), new PropertyMetadata(null));

public int RecordsCount
{
    get
    {
        return (int)GetValue(RecordsCountProperty);
    }
    set
    {
        SetValue(RecordsCountProperty, value);
    }
}
public static readonly DependencyProperty RecordsCountProperty = DependencyProperty.Register("RecordsCount", typeof(int), typeof(DataGridRecordsCounter), new PropertyMetadata(null));

public int PageSelected
{
    get
    {
        this.DataContext = this;
        return (int)GetValue(PageSelectedProperty);
    }
    set
    {
        this.DataContext = this;
        SetValue(PageSelectedProperty, value);
    }
}
public static readonly DependencyProperty PageSelectedProperty = DependencyProperty.Register("PageSelected", typeof(int), typeof(DataGridRecordsCounter), new PropertyMetadata(null));

public ObservableCollection<int> PagesList
{
    get
    {
        this.DataContext = this;
        return (ObservableCollection<int>)GetValue(PagesListProperty);
    }
    set
    {
        this.DataContext = this;
        SetValue(PagesListProperty, value);
    }
}
public static readonly DependencyProperty PagesListProperty = DependencyProperty.Register("PagesList", typeof(ObservableCollection<int>), typeof(DataGridRecordsCounter), new PropertyMetadata(null));

public int PagesCount
{
    get
    {
        return (int)GetValue(PagesCountProperty);
    }
    set
    {
        SetValue(PagesCountProperty, value);
    }
}

Вот часть моей страницы xaml:

<CustomControl:DataGridRecordsCounter
    RecordsCount="{Binding RowsCount, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True}" 
    RecordsPerPage="{Binding DGRecordsMax, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True}"  
    RecordsPerPageList="{Binding DGRecordsMaxList,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True }"  
    PagesCount="{Binding PagesCount,UpdateSourceTrigger=PropertyChanged ,Mode=TwoWay,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True}"
    PageSelected="{Binding CurrentPage,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True}"
    PagesList="{Binding PagesList,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True}"
    RecordsFrom="{Binding RecordsFrom, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True}"
    RecordsTo="{Binding RecordsTo, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True}">
</CustomControl:DataGridRecordsCounter>

и C# часть страницы

public int CurrentPage
{
    get
    {
        if (_currentPage < 1)
        {
            _currentPage = 1;
            RaisePropertyChanged("CurrentPage");
        }

        return _currentPage;
    }
    set
    {
        if (_currentPage != value)
        {
            _currentPage = value;
            RaisePropertyChanged("CurrentPage");
            Load();
        }
    }
}
private int _currentPage = 1;

public int TakeOffset
{
    get
    {
        return _takeOffset;
    }
    set
    {
        if (value != _takeOffset)
        {
            _takeOffset = value;
            RaisePropertyChanged("TakeOffset");
        }
    }
}
private int _takeOffset;

public int RowsCount
{
    get
    {
        return _rowsCount;
    }
    set
    {
        if (value != _rowsCount)
        {
            _rowsCount = value;
            RaisePropertyChanged("RowsCount");
        }
    }
}
private int _rowsCount;

public int DGRecordsMax
{
    get
    {
        return _dgRecordsMax;
    }
    set
    {
        if (value != _dgRecordsMax)
        {
            _dgRecordsMax = value;
            if (value > 0)
            {
                Mtrx.Manager.Properties.Settings.Default.DataGridRecordsMaxCount = value.ToString();
                Mtrx.Manager.Properties.Settings.Default.Save();
            }
            RaisePropertyChanged("DGRecordsMax");
            Load();
        }
    }
}
private int _dgRecordsMax;

public ObservableCollection<int> DGRecordsMaxList
{
    get
    {
        return _dGRecordsMaxList;
    }
    set
    {
        if (_dGRecordsMaxList != value)
        {
            _dGRecordsMaxList = value;
            Load();
            RaisePropertyChanged("DGRecordsMaxList");
        }
    }
}
private ObservableCollection<int> _dGRecordsMaxList = new ObservableCollection<int>();

public ObservableCollection<int> PagesList
{
    get
    {
        return _pagesList;
    }
    set
    {
        if (_pagesList != value)
        {
            _pagesList = value;
            Load();
            RaisePropertyChanged("PagesList");
        }
    }
}
private ObservableCollection<int> _pagesList = new ObservableCollection<int>();

public int PagesCount
{
    get
    {
        return _pagesCount;
    }
    set
    {
        if (value != _pagesCount)
        {
            _pagesCount = value;
            RaisePropertyChanged("PagesCount");
        }
    }
}
private int _pagesCount;

public int RecordsFrom
{
    get
    {
        return _recordsFrom;
    }
    set
    {
        if (value != _recordsFrom)
        {
            _recordsFrom = value;
            RaisePropertyChanged("RecordsFrom");
        }
    }
}
private int _recordsFrom;

public int RecordsTo
{
    get
    {
        return _recordsTo;
    }
    set
    {
        if (value != _recordsTo)
        {
            _recordsTo = value;
            RaisePropertyChanged("RecordsTo");
        }
    }
}
private int _recordsTo;

public IList<int> CurrentPageList
{
    get
    {
        return _currentPageList;
    }
    set
    {
        if (_currentPageList != value)
        {
            _currentPageList = value;

            Load();
            RaisePropertyChanged("CurrentPageList");
        }
    }
}
private IList<int> _currentPageList;

и метод загрузки:

private async void Load()
{
    AllowInput = false;

    PagesList.Clear();
    if (!PagesList.Contains(1))
    {
        PagesList.Add(1);
    }

    DGRecordsMax = int.Parse(Mtrx.Manager.Properties.Settings.Default.DataGridRecordsMaxCount);

    DGRecordsMaxList.Clear();

    List<int> RecordsMaxList = App.Properties.Settings.Default.DataGridRecordsMaxList.Split('|').Select(s => int.Parse(s)).ToList();

    foreach (int item in RecordsMaxList)
    {
        DGRecordsMaxList.Add(item);
    }

    int AllPages = (int)Math.Ceiling((double)RowsCount / (double)DGRecordsMax);

    IEnumerable<int> addPagesList = Enumerable.Range(1, AllPages).ToList<int>();

    foreach (int item in addPagesList)
    {
        if (item != 1)
        {
            PagesList.Add(item);
        }
    }
}

Может кто-нибудь объяснить, как все время отображать эти значения в поле со списком.

...