Xaml 将字典传递给行为

Xaml 将字典传递给行为,xaml,c#-4.0,silverlight-4.0,behavior,attachedbehaviors,Xaml,C# 4.0,Silverlight 4.0,Behavior,Attachedbehaviors,我有这样一个简单的行为 public class KeyBoardChangeBehavior : Behavior<UserControl> { public Dictionary<string, int> DataToCheckAgainst; protected override void OnAttached() { AssociatedObject.KeyDown += _KeyBoardBehaviorKeyDow

我有这样一个简单的行为

public class KeyBoardChangeBehavior : Behavior<UserControl>
{
    public Dictionary<string, int> DataToCheckAgainst; 


    protected override void OnAttached()
    {
        AssociatedObject.KeyDown += _KeyBoardBehaviorKeyDown;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.KeyDown -= _KeyBoardBehaviorKeyDown;
    }


        void _KeyBoardBehaviorKeyDown(object sender, KeyEventArgs e)
    {
        // My business will go there 
    }

}
公共类键盘更改行为:行为
{
公共字典数据到checkagainst;
受保护的覆盖无效附加()
{
AssociatedObject.KeyDown+=\u键盘行为向下;
}
附加时受保护的覆盖无效()
{
AssociatedObject.KeyDown-=\u键盘行为向下;
}
void _KeyBoardBehaviorKeyDown(对象发送方,KeyEventArgs e)
{
//我的生意要到那里去
}
}
我想从视图中为这个字典赋值,我称它为

<UserControl x:Class="newhope2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:Behaviors="clr-namespace:newhope2"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Interactivity:Interaction.Behaviors>
        <Behaviors:KeyBoardChangeBehavior  />
    </Interactivity:Interaction.Behaviors>

    <Grid x:Name="LayoutRoot" Background="White">

    </Grid>
</UserControl>


但是如何将该字典从XAML或其隐藏代码传递给行为,您需要做的就是将字典声明为属性,然后通过绑定传递一个值

在行为方面:

public Dictionary<string, int> DataToCheckAgainst { get; set; }
public Dictionary DataToCheckAgainst{get;set;}
在XAML中:

<Interactivity:Interaction.Behaviors>
    <Behaviors:KeyBoardChangeBehavior DataToCheckAgainst="{Binding MyDictionary}" />
</Interactivity:Interaction.Behaviors>

只需将字典声明为属性,然后通过绑定传递一个值

在行为方面:

public Dictionary<string, int> DataToCheckAgainst { get; set; }
public Dictionary DataToCheckAgainst{get;set;}
在XAML中:

<Interactivity:Interaction.Behaviors>
    <Behaviors:KeyBoardChangeBehavior DataToCheckAgainst="{Binding MyDictionary}" />
</Interactivity:Interaction.Behaviors>

要进行绑定,属性必须是
从属属性

您需要在行为中定义属性,如下所示:

    public Dictionary<string, int> DataToCheckAgainst
    {
        get { return (Dictionary<string, int>)GetValue(DataToCheckAgainstProperty); }
        set { SetValue(DataToCheckAgainstProperty, value); }
    }

    public static readonly DependencyProperty DataToCheckAgainstProperty =
        DependencyProperty.Register(
            "DataToCheckAgainst",
            typeof(Dictionary<string, int>),
            typeof(KeyBoardChangeBehavior),
            new PropertyMetadata(null));
<Interactivity:Interaction.Behaviors>
    <Behaviors:KeyBoardChangeBehavior DataToCheckAgainst="{Binding MyDictionary}" />
</Interactivity:Interaction.Behaviors>
public Dictionary DataToCheckAgainst
{
获取{return(Dictionary)GetValue(DataToCheckAgainstProperty);}
set{SetValue(DataToCheckAgainstProperty,value);}
}
public static readonly dependencProperty数据到checkagainstProperty=
从属属性。寄存器(
“DataToCheckAgainst”,
类型(字典),
类型(键盘切换行为),
新属性元数据(空);
使用VisualStudio“propdp”代码段

用法如Adi所说,如下所示:

    public Dictionary<string, int> DataToCheckAgainst
    {
        get { return (Dictionary<string, int>)GetValue(DataToCheckAgainstProperty); }
        set { SetValue(DataToCheckAgainstProperty, value); }
    }

    public static readonly DependencyProperty DataToCheckAgainstProperty =
        DependencyProperty.Register(
            "DataToCheckAgainst",
            typeof(Dictionary<string, int>),
            typeof(KeyBoardChangeBehavior),
            new PropertyMetadata(null));
<Interactivity:Interaction.Behaviors>
    <Behaviors:KeyBoardChangeBehavior DataToCheckAgainst="{Binding MyDictionary}" />
</Interactivity:Interaction.Behaviors>

要进行绑定,属性必须是
从属属性

您需要在行为中定义属性,如下所示:

    public Dictionary<string, int> DataToCheckAgainst
    {
        get { return (Dictionary<string, int>)GetValue(DataToCheckAgainstProperty); }
        set { SetValue(DataToCheckAgainstProperty, value); }
    }

    public static readonly DependencyProperty DataToCheckAgainstProperty =
        DependencyProperty.Register(
            "DataToCheckAgainst",
            typeof(Dictionary<string, int>),
            typeof(KeyBoardChangeBehavior),
            new PropertyMetadata(null));
<Interactivity:Interaction.Behaviors>
    <Behaviors:KeyBoardChangeBehavior DataToCheckAgainst="{Binding MyDictionary}" />
</Interactivity:Interaction.Behaviors>
public Dictionary DataToCheckAgainst
{
获取{return(Dictionary)GetValue(DataToCheckAgainstProperty);}
set{SetValue(DataToCheckAgainstProperty,value);}
}
public static readonly dependencProperty数据到checkagainstProperty=
从属属性。寄存器(
“DataToCheckAgainst”,
类型(字典),
类型(键盘切换行为),
新属性元数据(空);
使用VisualStudio“propdp”代码段

用法如Adi所说,如下所示:

    public Dictionary<string, int> DataToCheckAgainst
    {
        get { return (Dictionary<string, int>)GetValue(DataToCheckAgainstProperty); }
        set { SetValue(DataToCheckAgainstProperty, value); }
    }

    public static readonly DependencyProperty DataToCheckAgainstProperty =
        DependencyProperty.Register(
            "DataToCheckAgainst",
            typeof(Dictionary<string, int>),
            typeof(KeyBoardChangeBehavior),
            new PropertyMetadata(null));
<Interactivity:Interaction.Behaviors>
    <Behaviors:KeyBoardChangeBehavior DataToCheckAgainst="{Binding MyDictionary}" />
</Interactivity:Interaction.Behaviors>


我忘记使用依赖属性而不是普通属性来绑定工作@Duncan的解决方案就是这样。谢谢,如果我想以相同的方式传递文本框的文本,该怎么办?我不确定您指的是什么文本框,但您可以更改绑定,使其绑定到文本框(使用ElementName或RelativeSource)。我忘记使用依赖属性而不是普通属性来绑定工作@Duncan的解决方案就是这样。谢谢,如果我想以相同的方式传递文本框的文本,该怎么办?我不确定您指的是什么文本框,但您可以更改绑定,使其绑定到文本框(使用ElementName或RelativeSource)。谢谢,如果我想以相同的方式传递文本框的文本,您需要添加另一个依赖属性string,然后绑定到文本框绑定到的同一属性,或者,如果文本框没有绑定到任何内容,则使用ElementName绑定直接绑定到文本框,该怎么办。我建议阅读关于绑定的文档:如何在XAML pleasethanks上定义此字典,如果我想以相同的方式传递TextBox的文本,您需要添加另一个依赖属性string,然后绑定到文本框绑定到的同一属性,或者,如果文本框没有绑定到任何内容,该怎么办,使用ElementName绑定直接绑定到文本框。我建议通读关于绑定的文档:如何在XAML上定义这本词典