【问题标题】:How can I use combobox with wpf mvvm如何将组合框与 wpf mvvm 一起使用
【发布时间】:2020-07-02 00:55:07
【问题描述】:

我有一张员工表。和员工表中的位置字段。 我必须使用组合框来过滤它。如果我在组合框中选择“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 ParticularEntries

<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 相关的部分: 组合框:

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 或其他方面进行哪些更改才能做到这一点?

【问题讨论】:

  • 在 xaml 的组合框中,我使用带有 SelectionChanged 事件的命令 LocationFilter 绑定。
  • 首先我用 LocationFilterCommand 的初始化编辑了我的问题。 ParticularEntries 是我的所有员工条目(A 位置和 B 位置一起),当我在组合框中选择 A 时,我希望只有 A 位置员工应该进入屏幕。我在 LocationFilterCommand 中编写了 select 方法,例如“ParticularEntries.Select(pg => pg.Region.Location == _selectedLocation);”
  • 这可能是错误的。我需要你的帮助才能有正确的观点
  • 我再次编辑了我的问题。我是 WPF 的新手,感谢您的宝贵时间
  • 好的。我学会了如何接受答案 :) 我也是新的 stackoverflow@Sir Rufo

标签: c# wpf mvvm combobox command


【解决方案1】:

LocationFilter 中的代码毫无意义。

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

它返回一个IEnumerable&lt;bool&gt;,但它从未被分配。

如果要过滤,必须使用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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-11
    • 2011-10-08
    • 2014-03-30
    • 1970-01-01
    • 2012-07-20
    • 2013-05-29
    • 2017-09-02
    • 1970-01-01
    相关资源
    最近更新 更多