簡體   English   中英

如何將組合框與 wpf mvvm 一起使用

[英]How can I use combobox with wpf mvvm

我有一張員工表。 和員工表中的位置字段。 我必須使用組合框來過濾它。 如果我在組合框中選擇“A 位置”,則只有 A 位置的人應該進入屏幕,如果我選擇 B 位置,則只有 B 位置的人應該進入屏幕。 這是我的 xaml 條目和 ComboBox.ParticularEntries 是我的所有條目(A 和 B 位置在一起)

像這樣初始化 ParticularEntries:

private IEnumerable<EntryReportParticular> _particularEntries;
public IEnumerable<EntryReportParticular> ParticularEntries
{
    get { return _particularEntries; }
    set { Set(ref _particularEntries, value); }
}

和 EntryReport 特定模型類:

public class EntryReportParticular : BindableItem
{
    private Employee _employee;
    public Employee Employee
    {
        get { return _employee; }
        set { Set(ref _employee, value); }
    }

    private DateTime _entry;
    public DateTime Entry
    {
        get { return _entry; }
        set { Set(ref _entry, value, () => OnPropertyChanged(nameof(Duration))); }
    }

    private DateTime _exit;
    public DateTime Exit
    {
        get { return _exit; }
        set { Set(ref _exit, value, () => OnPropertyChanged(nameof(Duration))); }
    }

    public TimeSpan Duration { get { return Exit - Entry; } }

    private Region _region;
    public Region Region
    {
        get { return _region; }
        set { Set(ref _region, value); }
    }
}

這是我的 xaml 特殊條目

<DataGrid  
    ItemsSource="{Binding ParticularEntries}" 
    AutoGenerateColumns="False" 
    IsReadOnly="True" 
    RowHeaderWidth="0" 
    GridLinesVisibility="All"
    HorizontalGridLinesBrush="WhiteSmoke"
    VerticalGridLinesBrush="WhiteSmoke" 
    Margin="4">

這是我的帶命令的組合框。

<ComboBox 
    ItemsSource="{Binding Locations}" 
    SelectedItem ="{Binding SelectedLocation}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding LocationFilterCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

這是我與 ViewModel 相關的部分:ComboBox:

private string _selectedLocation;
public string SelectedLocation
{
    get { return _selectedLocation; }
    set
    {
        _selectedLocation = value;
        OnPropertyChanged("SelectedLocation");
        Trace.WriteLine(SelectedLocation);
    }
}

private ObservableCollection<string> _locations;
public ObservableCollection<string> Locations
{
    get { return _locations; }
    set
    {
        _locations = value;
        OnPropertyChanged("Locations");
    }
}

public EntryReportViewModel()//Constructor
{
    Locations = new ObservableCollection<string>()
    {
        "A Location","B Location"
    };
}

LocationFilterCommand(根據位置過濾,沒有按鈕)

#region LocationFilterCommand

private DelegateCommand _locationFilterCommand;
public DelegateCommand LocationFilterCommand
{
    get { return _locationFilterCommand ?? (_locationFilterCommand = new DelegateCommand(CanLocationFilter, LocationFilter)); }
}

private bool CanLocationFilter()
{

    if (ParticularEntries == null || DailyEntries == null || MonthlyEntries == null)
        return false;

    return true;
}
private void LocationFilter()
{
   ParticularEntries.Select(pg => pg.Region.Location == _selectedLocation);
   MonthlyEntries.Select(pg => pg.Employee.CostCenter.Location == _selectedLocation);

}
#endregion

我就是這么做的。 我有帶有 A 和 B 位置的 ComboBox,但是當我選擇 A 或 B 位置時,任何內容都發生了變化。我該如何解決這個問題以及如何根據位置進行過濾? 我應該在 UI 或其他方面更改什么才能做到這一點?

您在LocationFilter代碼根本沒有意義。

ParticularEntries.Select(pg => pg.Region.Location == _selectedLocation);

它返回一個IEnumerable<bool>但它從未被分配。

如果要過濾,則必須使用Where

但即使您將代碼更改為

ParticularEntries = ParticularEntries.Where(pg => pg.Region.Location == _selectedLocation);

您會看到變化,但下次選擇不同位置時將面臨下一個問題。

解決方案

您需要一個包含存儲在私有字段中的所有未過濾項目的集合,並將其用於過濾。

private IEnumerable<EntryReportParticular> _allEntries;

private IEnumerable<EntryReportParticular> _particularEntries;
public IEnumerable<EntryReportParticular> ParticularEntries
{
    get { return _particularEntries; }
    set { Set(ref _particularEntries, value); }
}

private void LocationFilter()
{
   ParticularEntries = _allEntries
       .Where(pg => pg.Region.Location == _selectedLocation)
       .ToList();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM