从已知的名单限制WPF组合框的选择 [英] Restricting WPF ComboBox choices from a known list

查看:154
本文介绍了从已知的名单限制WPF组合框的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个包含组合框一个WPF用户控件。组合框选择是时间单位,即秒,分,小时,天等,并通过一个数据库查找表中提供。每一个选择是依赖于从所谓的TimeInterval所提供枚举相应的枚举值。我们的应用程序会根据选择的选择决定的其他功能。

I'm developing a WPF user control that contains a ComboBox. The ComboBox choices are units of time, i.e. seconds, minutes, hours, days, etc. and are provided via a database lookup table. Each choice is tied to a corresponding enum value available from an enum called TimeInterval. Other features of our application will make decisions based on the choice selected.

开发需要能够以限制可作为选择的时间单位。例如,在某些情况下,可能仅是适当的,以显示在列表分和秒。其他的开发人员可能需要提供整个列表。

Developers using this control need to be able to limit which units of time are available as choices. For example, in certain circumstances it may only be appropriate to show minutes and seconds in the list. Other developers may want to provide the entire list.

我试图想出这样的一个优雅的方式,但很茫然。我首先想到的就是以某种方式使用标志(的FlagsAttribute)在一个枚举,但我不知道如何处理它。

I'm trying to come up with an elegant way of doing this, but am at a loss. My first thought was to somehow use Flags (FlagsAttribute) over an enum, but I wasn't sure how to approach it.

一个纯粹的XAML的方法将是理想的,但我开到code解决方案,以及

A pure XAML approach would be ideal, but I'm open to code solutions as well.

先谢谢了。

推荐答案

您可以使用的CollectionView的优势,实现预期的行为。

You could use advantages of CollectionView to achieve expected behaviour.

在这种情况下你的用户控件公开FilterTimeInterval依赖属性,其类型为 predicate<&TimeInterval所GT; 。当此属性更改,您设置了 ICollectionView.Filter 属性的项目集合。如果第三方开发者使用你的控制和欲望来限制组合框的物品,他应该只是结合适当predicate到FilterTimeInterval财产。

In such scenario your UserControl exposes FilterTimeInterval dependency property, which type is Predicate<TimeInterval>. When this property is changed, you set up ICollectionView.Filter property for collection of your items. If third-party developer uses your control and desires to restrict combobox items, he should just bind appropriate predicate to FilterTimeInterval property.

下面是一些示范(为了简化编码,组合框包含字符串项)。

Here is some demonstration (in order to simplify coding, combobox contains string items).

你的控制XAML:

<UserControl x:Class="Test.TimeIntervalsControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <ComboBox ItemsSource="{Binding Items}"/>
    </Grid>
</UserControl>

与依赖属性声明你的控制背后

code:

Code behind of your control with dependency property declaration:

public partial class TimeIntervalsControl : UserControl
{
    public TimeIntervalsControl()
    {
        InitializeComponent();
        this.Model = new TimeIntervalsViewModel();
    }

    public TimeIntervalsViewModel Model
    {
        get
        {
            return (TimeIntervalsViewModel)this.DataContext;
        }
        set
        {
            this.DataContext = value;
        }
    }

    public Predicate<string> FilterTimeInterval
    {
        get
        {
            return (Predicate<string>)this.GetValue(TimeIntervalsControl.FilterTimeIntervalProperty);
        }
        set
        {
            this.SetValue(TimeIntervalsControl.FilterTimeIntervalProperty, value);
        }
    }

    public static readonly DependencyProperty FilterTimeIntervalProperty =
        DependencyProperty.Register("FilterTimeInterval",
            typeof(Predicate<string>),
            typeof(TimeIntervalsControl),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.None,   
                (d, e) =>
                {
                    var control = (TimeIntervalsControl)d;
                    var view = CollectionViewSource.GetDefaultView(control.Model.Items);
                    if (e.NewValue == null)
                    {
                        view.Filter = null;
                    }
                    else
                    {
                        view.Filter = o => ((Predicate<string>)e.NewValue)((string)o);
                    }
                }));
}

你的控制视图模型:

ViewModel of your control:

public sealed class TimeIntervalsViewModel : ObservableObject
{
    private readonly string[] _items = new string[] {"Years","Month","Days","Hours","Minutes","Seconds"};

    public IEnumerable<string> Items
    {
        get
        {
            return this._items;
        }
    }
}

控制已准备就绪,现在是时候使用它,并限制一些项目。在这个例子中,我阻止除天每一个项目。

Control is ready, it's time to use it and restrict some items. In this example I block every item except "Days".

在XAML控件的使用方法如下:

Usage of your control in XAML looks like:

<UserControl x:Class="Test.StartupView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:self="clr-namespace:Test"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Name="Root">
    <WrapPanel>
        <self:TimeIntervalsControl FilterTimeInterval="{Binding Path=DataContext.JustDaysFilter, ElementName=Root}"/>
    </WrapPanel>
</UserControl>

当然,我们应该$鉴于P型$ ppare JustDaysFilter属性:

And of course we should prepare JustDaysFilter property in view model:

public class StartupViewModel
{
    private readonly Predicate<string> _justDaysFilter = s => s == "Days";
    public Predicate<string> JustDaysFilter
    {
        get
        {
            return this._justDaysFilter;
        }
    }
}

这篇关于从已知的名单限制WPF组合框的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆