开发者

Design-time problem with custom behaviors and triggers in Silverlight

I have implemented a few custom behaviors and开发者_开发问答 triggers and added them via XAML. They work fine at run time but prevent the Cider designer view from loading at design time, and presumably will cause a problem in Blend too, though I haven't confirmed that.

Here is an overview of what I've implemented for one of the behaviors; hopefully someone can point out what I'm missing.

The behavior looks like this;

using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interactivity;

namespace MiX.Core.UI.Silverlight
{
    public class UpdateOnTextChangedBehavior : Behavior<TextBox>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.TextChanged += OnAssociatedObjectTextChanged;
        }

        void OnAssociatedObjectTextChanged(object sender, TextChangedEventArgs e)
        {
            BindingExpression binding = this.AssociatedObject.GetBindingExpression(TextBox.TextProperty);
            if (binding != null)
            {
                binding.UpdateSource();
            }
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            this.AssociatedObject.TextChanged -= OnAssociatedObjectTextChanged;
        }
    }
}

An implementation in XAML looks like this;

<TextBox x:Name="Username" Text="{Binding Username,Mode=TwoWay}" BorderThickness="1" Style="{StaticResource TextBoxStyleGeneral}" Foreground="#FF333333" FontSize="10" BorderBrush="{x:Null}" Grid.Column="1" d:LayoutOverrides="GridBox" Margin="2,0" Grid.ColumnSpan="2" Background="{x:Null}" VerticalAlignment="Center" Grid.Row="1">
  <i:Interaction.Behaviors>
    <mixcore:UpdateOnTextChangedBehavior/>
  </i:Interaction.Behaviors>
</TextBox>

In the XAML editor the <mixcore:UpdateOnPasswordChangedBehavior/> element is highlighted with a squiggly and reports the error A value of type 'UpdateOnTextChangedBehavior' cannot be added to a collection or dictionary of type 'BehaviorCollection'. When attempting to view in the Design view the designer fails to load, stating The document contains errors that must be fixed before the designer can be loaded.


In Silverlight ,If design is not able to load with the changes we made in code then it is bug in silverlight.

Silverlight yet is not designed to handle various exceptions through code,like if you do have any code with return type and you don't check for null there,then again it doesn't load the designer this case is mostly seen with Overriding the IValueConverter method {x:Static} ...and so on

There is nothing wrong in your code unless until it compile fine and doesn't throw exception. Don't worry about the designer.

Similarly,one case you can look at: http://connect.microsoft.com/VisualStudio/feedback/details/361509/xaml-designer-cannot-handle-typename-with-nested-classes

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜