C# 分离后无法将行为重新附加到依赖项对象

C# 分离后无法将行为重新附加到依赖项对象,c#,wpf,silverlight,blend,C#,Wpf,Silverlight,Blend,在运行时生成UIElement时,我将行为(Blend SDK中的行为)附加到UIElement。我还有两个按钮来触发Detach()和Attach()——基本上是为了启用或禁用该行为 问题是:在IDetach()该行为之后,Attach()未能将该行为恢复到依赖项对象,而依赖项对象的行为仍然较少: // Declare the dependency object and behavior at class scope MyControl c = new MyControl(); MyBeha

在运行时生成UIElement时,我将行为(Blend SDK中的行为)附加到UIElement。我还有两个按钮来触发Detach()Attach()——基本上是为了启用或禁用该行为

问题是:在IDetach()该行为之后,Attach()未能将该行为恢复到依赖项对象,而依赖项对象的行为仍然较少:

// Declare the dependency object and behavior at class scope 
MyControl c = new MyControl();
MyBehavior b = new MyBehavior();

// Function that generate UI Controls with behavior attached
b.Attach(c);

// Function that detach behavior from dependency object
b.Detach();

// Function that re-attach behavior to the same object after it has been detached
b.Attach(c); // <-- Do not see the behavior...
//在类范围内声明依赖项对象和行为
MyControl c=新的MyControl();
MyBehavior b=新的MyBehavior();
//用于生成附加了行为的UI控件的函数
b、 附上(c);
//从依赖项对象分离行为的函数
b、 分离();
//函数,用于在分离同一对象后将行为重新附加到该对象

b、 附(c);// 问题似乎与你的行为逻辑有关。在以下测试中,行为重新附着时不会出现问题:

public class ColorBehavior : Behavior<Border>
{
    public Brush OriginalBrush { get; set; }

    protected override void OnAttached()
    {
        base.OnAttached();

        this.OriginalBrush = this.AssociatedObject.Background;
        this.AssociatedObject.Background = Brushes.CadetBlue;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        this.AssociatedObject.Background = this.OriginalBrush;
    }
}

public partial class MainWindow : Window
{
    private ColorBehavior behavior = new ColorBehavior();
    private bool isAttached;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (!this.isAttached)
        {
            this.behavior.Attach(this.Border);
            this.isAttached = true;
        }
        else
        {
            this.behavior.Detach();
            this.isAttached = false;
        }
    }
}

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow"
    Width="525"
    Height="350">
<Grid>
    <Border x:Name="Border" Background="Red" />
    <Button Width="50"
            Height="20"
            Click="Button_Click"
            Content="Hey" />
</Grid>
公共类ColorBehavior:行为
{
公共笔刷原始笔刷{get;set;}
受保护的覆盖无效附加()
{
base.onatached();
this.OriginalBrush=this.AssociatedObject.Background;
this.AssociatedObject.Background=笔刷.CadetBlue;
}
附加时受保护的覆盖无效()
{
base.OnDetaching();
this.AssociatedObject.Background=this.OriginalBrush;
}
}
公共部分类主窗口:窗口
{
私有ColorBehavior=新ColorBehavior();
私人厕所已关闭;
公共主窗口()
{
初始化组件();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
如果(!this.isAttached)
{
this.behavior.Attach(this.Border);
this.isAttached=true;
}
其他的
{
this.behavior.Detach();
this.isAttached=false;
}
}
}

问题似乎与你的行为逻辑有关。在以下测试中,行为重新附着时不会出现问题:

public class ColorBehavior : Behavior<Border>
{
    public Brush OriginalBrush { get; set; }

    protected override void OnAttached()
    {
        base.OnAttached();

        this.OriginalBrush = this.AssociatedObject.Background;
        this.AssociatedObject.Background = Brushes.CadetBlue;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        this.AssociatedObject.Background = this.OriginalBrush;
    }
}

public partial class MainWindow : Window
{
    private ColorBehavior behavior = new ColorBehavior();
    private bool isAttached;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (!this.isAttached)
        {
            this.behavior.Attach(this.Border);
            this.isAttached = true;
        }
        else
        {
            this.behavior.Detach();
            this.isAttached = false;
        }
    }
}

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow"
    Width="525"
    Height="350">
<Grid>
    <Border x:Name="Border" Background="Red" />
    <Button Width="50"
            Height="20"
            Click="Button_Click"
            Content="Hey" />
</Grid>
公共类ColorBehavior:行为
{
公共笔刷原始笔刷{get;set;}
受保护的覆盖无效附加()
{
base.onatached();
this.OriginalBrush=this.AssociatedObject.Background;
this.AssociatedObject.Background=笔刷.CadetBlue;
}
附加时受保护的覆盖无效()
{
base.OnDetaching();
this.AssociatedObject.Background=this.OriginalBrush;
}
}
公共部分类主窗口:窗口
{
私有ColorBehavior=新ColorBehavior();
私人厕所已关闭;
公共主窗口()
{
初始化组件();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
如果(!this.isAttached)
{
this.behavior.Attach(this.Border);
this.isAttached=true;
}
其他的
{
this.behavior.Detach();
this.isAttached=false;
}
}
}

您提醒我,我正在使用属于第三方DLL的行为,我必须在源代码处进行修改以更正某些问题。问题解决了。谢谢。你提醒我,我正在使用一个属于第三方DLL的行为,我必须在源代码处修改以更正某些问题。问题解决了。谢谢