将枚举值绑定到XAML中的附加属性

时间:2014-05-26 04:52:52

标签: c# wpf xaml enums attached-properties

我有以下具有枚举和附加属性的类。

namespace U.Helpers
{
    public enum SearchDirection
    {
        Forward,
        Backward
    }
    public class TargetedTriggerActionFindNextButton : TargetedTriggerAction<DataGrid>
    {
        protected override void Invoke(object parameter)
        {
            if (SearchDirectionControl == SearchDirection.Forward)
                //Do something
            else
                //Do something else
            }

        public static readonly DependencyProperty SearchDirectionControlProperty =
            DependencyProperty.Register("SearchDirectionControl", typeof(object), typeof(TargetedTriggerActionFindNextButton), new PropertyMetadata(SearchDirection.Forward));

        public SearchDirection SearchDirectionControl
        {
            get { return (SearchDirection)GetValue(SearchDirectionControlProperty); }
            set { SetValue(SearchDirectionControlProperty, value); }
        }
    }
}

到目前为止,这是我的XAML:

<UserControl x:Class="UM.LaunchPad"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:helpers="clr-namespace:UM.Helpers">

    <Grid Name="gridUsers" Background="Transparent">
        <Button Name="SearchNextButton" Content="Next" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <helpers:TargetedTriggerActionFindNextButton TargetObject="{Binding ElementName=GenericDataGrid}" 
                             SearchDirectionControl="{Binding helpers:SearchDirection.Forward}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
    </Grid>
</UserControl>

我想知道如何在XAML端为我的附加属性添加枚举值。查看按钮的SearchDirectionControl。

1 个答案:

答案 0 :(得分:2)

如果要引用特定的枚举值,请不要使用绑定,而是使用x:Static Markup Extension代替:

<helpers:TargetedTriggerActionFindNextButton
    TargetObject="{Binding ElementName=GenericDataGrid}" 
    SearchDirectionControl="{x:Static helpers:SearchDirection.Forward}"
/>


引用 x:静态MSDN documentation

  

引用以符合公共语言规范(CLS)的方式定义的任何静态按值代码实体。   
[...]   
引用的代码实体必须是以下之一:

A constant
A static property
A field   [sic; it should be a "static field", obviously]
An enumeration value