【问题标题】:WPF multiple-condition binding filteringWPF 多条件绑定过滤
【发布时间】:2017-07-05 12:10:26
【问题描述】:

我是 WPF 和所有这些神奇的绑定和数据触发器的新手,所以我请你帮忙。

我有一个简单的 wpf 应用程序,如下图所示。

我希望我的数据网格内容反映条件和日期过滤器。我已经想出了如何根据事件代码和复选框(开始、停止、错误)绑定数据网格行的可见性。但我不知道如何实现日期过滤。我想要的是:当“按日期过滤”复选框被选中时,在我的数据网格中只有那些行保持可见,它们在“服务器时间”字段中有日期(我想我需要从日期时间以某种方式解析它)等于选定的日期组合框. 我可以仅使用 xaml 来实现吗?谁能帮我做到这一点? 这是我的数据网格的 xaml:

<DataGrid
        Grid.Row="1"
        Margin="5"
        AutoGenerateColumns="False"
        IsReadOnly="True"
        ItemsSource="{Binding LogEntries}"
        Style="{DynamicResource Helvetica}">
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding event_code}" Value="1">
                        <Setter Property="Background" Value="LightGreen" />
                        <Setter Property="Visibility" Value="{Binding IsChecked, ElementName=StartShowChecked, Converter={StaticResource BooleanToVisibilityConverter}}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding event_code}" Value="2">
                        <Setter Property="Background" Value="LightGray" />
                        <Setter Property="Visibility" Value="{Binding IsChecked, ElementName=StopShowChecked, Converter={StaticResource BooleanToVisibilityConverter}}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding event_code}" Value="3">
                        <Setter Property="Background" Value="#FFEA816F" />
                        <Setter Property="Visibility" Value="{Binding IsChecked, ElementName=ErrorShowChecked, Converter={StaticResource BooleanToVisibilityConverter}}" />
                    </DataTrigger>

                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding server_datetime, StringFormat=\{0:dd.MM.yy HH:mm:ss\}}" Header="Server time" />
            <DataGridTextColumn Binding="{Binding user_datetime, StringFormat=\{0:dd.MM.yy HH:mm:ss\}}" Header="Client time" />
            <DataGridTextColumn
                Width="*"
                Binding="{Binding log_entry}"
                Header="Entry" />

        </DataGrid.Columns>

【问题讨论】:

  • 只需使用复选框事件并在后面的代码中更新您的 ObservableCollection。

标签: wpf xaml binding datagrid


【解决方案1】:

我可以仅使用 xaml 来实现吗?

不,您不能,因为 XAML 是一种标记语言,仅此而已。

您要做的是将日期ComboBoxSelectedItem 绑定到视图模型的DateTime 属性,并将“过滤依据”复选框的IsChecked 属性绑定到bool 属性当设置IsChecked 源属性时,您的视图模型和过滤LogEntries 源集合,例如:

public class ViewModel : INotifyPropertyChanged
{

    private bool _isChecked;
    public bool IsChecked
    {
        get { return _isChecked; }
        set
        {
            _isChecked = value;
            OnPropertyChanged();
            //filter collection:
            LogEntries = allLogEntries.Where(x => x.ServerTime == SelectedDate).ToList();
        }
    }

    private List<LogEntry> _logEntries;
    public List<LogEntry LogEntries
    {
        get { return _logEntries; }
        set
        {
            _logEntries = value;
            OnPropertyChanged();
        }
    }

    //...

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

【讨论】:

    【解决方案2】:

    我怀疑只有在 XAML 中才能做到这一点,因为过滤需要您指定如何过滤(例如通过创建谓词)。
    我建议从您的 ItemsSource 获取 ICollectionView(我假设它是 ObservableCollection)并将其设置为 Filter 属性。
    查看此答案以获取更多详细信息:Filter a DataGrid in WPF

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 2011-01-06
      • 2013-06-20
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      相关资源
      最近更新 更多