如何将<collection>作为参数传递给Markup扩展的构造函数

时间:2015-07-15 08:49:36

标签: wpf xaml

I have enum which i need to bind to combobox.

public enum RuleType
    {
        Default,
        Required,
        Hide,
        Disable,
        Validate,
        Calculate,
        Maxlength,
        Trigger
    }

// array of this class is return type of extension

公共类EnumMember     {             公共对象价值{get;组; }             public string Discription {get;组; }     }

我已经使用标记扩展来直接在xaml中绑定上面的枚举:

    [MarkupExtensionReturnType(typeof(Array))]
    public class EnumValuesExtension : MarkupExtension
    {
        public EnumValuesExtension()
        {
        }
        public EnumValuesExtension(Type enumType)
        {
            if (enumType == null)
                throw new ArgumentNullException("enumType");

            this.EnumType = enumType;
            // this.Excluded = filterCollection;
        }

        public EnumValuesExtension(Type enumType, RuleType filterCollection)
        {
            if (enumType == null)
                throw new ArgumentNullException("enumType");

            this.EnumType = enumType;
            this.Excluded = filterCollection;
        }
        public Type EnumType
        {
            get { return _enumType; }
            set
            {
                if (_enumType == value)
                    return;
                _enumType = value;
            }
        }

 // Returning all elements of enum
 public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return from Object enumValue in Enum.GetValues(EnumType)
                   select new EnumMember
                   {
                       Value = enumValue,
                       Discription = enumValue.ToString()
                   };
        }
    }
}

XAML绑定是:

// "FilterDefaultElement" is Dependency Property which retrieve all enum elements.
<Style x:Key="filterRuleCombo" TargetType="{x:Type ComboBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=radio0,Path=IsChecked}" Value="True">
                    <Setter Property="ItemsSource" 
                            Value="{Binding FilterDefaultElement}"/>                    
                </DataTrigger>
 </Style.Triggers>
        </Style>
    </Window.Resources>

<ComboBox Grid.Column="0" Grid.Row="1"  Style="{StaticResource filterRuleCombo}"                
                  DisplayMemberPath="Discription" Margin="0,5,0,100">  
        </ComboBox> 

我的问题是:

我想编写像

这样的扩展构造函数
 public EnumValuesExtension(Type enumType, ObservableCollection<EnumMember> obj)
 {
 }

应该能够接受集合参数。 我只能将一个参数传递给构造函数。

如何将“ObservableCollection obj”作为参数传递给标记扩展的构造函数?

0 个答案:

没有答案