簡體   English   中英

將枚舉值綁定到XAML中的附加屬性

[英]Bind enum value to attached property in XAML

我有以下具有枚舉和附加屬性的類。

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。

當您要引用特定的Enum值時,請勿使用綁定,而應使用x:Static Markup Extension

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


引用x:StaticMSDN文檔

引用以符合公共語言規范(CLS)的方式定義的任何靜態按值代碼實體。
[...]
被引用的代碼實體必須是以下之一:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM