Button 控件上的 Silverlight ChangePropertyAction 不起作用

标签 silverlight silverlight-4.0 expression-blend silverlight-5.0

开箱即用,如果你启动 Blend,在网格上放置一个按钮并在其上设置几个 ChangeProeprtyActions ... 将失败并显示错误消息。

<UserControl
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:ei="http://schemas.microsoft.com/expression/2010/interactions"
x:Class="SL4_Button_States.MainPage"
Width="640" Height="480">

<Grid x:Name="LayoutRoot" Background="White">
    <Button x:Name="button" Content="Button" Height="39" Margin="68,42,299,0" VerticalAlignment="Top">
        <i:Interaction.Triggers>
            <i:EventTrigger>
                <ei:ChangePropertyAction PropertyName="Content" Value="Button is now loaded!" TargetObject="{Binding ElementName=button}"/>
            </i:EventTrigger>
            <i:EventTrigger EventName="Click">
                <ei:ChangePropertyAction PropertyName="Content" Value="temporary"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
</Grid>

错误信息是这样的:

    Webpage error details

Message: Unhandled Error in Silverlight Application The type 'Object' was not found. [Line: 2 Position: 10]   at MS.Internal.XcpImports.CreateFromXaml(String xamlString, Boolean createNamescope, Boolean requireDefaultNamespace, Boolean allowEventHandlers, Boolean expandTemplatesDuringParse)
   at MS.Internal.XcpImports.CreateFromXaml(String xamlString, Boolean createNamescope, Boolean requireDefaultNamespace, Boolean allowEventHandlers)
   at Microsoft.Expression.Interactivity.TypeConverterHelper.ExtendedStringConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at Microsoft.Expression.Interactivity.Core.ChangePropertyAction.Invoke(Object parameter)
   at System.Windows.Interactivity.TriggerBase.InvokeActions(Object parameter)
   at System.Windows.Interactivity.EventTriggerBase.OnEvent(EventArgs eventArgs)
   at System.Windows.Interactivity.EventTriggerBase.OnEventImpl(Object sender, EventArgs eventArgs)
   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(UInt32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)
Line: 1
Char: 1
Code: 0
URI: http://localhost:62193/Silverlight.js

如果您在 TextBlock 上添加 ChangePropertyAction ... 工作正常:

<TextBlock Height="20" Margin="207,132,138,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseLeftButtonDown">
                <ei:ChangePropertyAction PropertyName="Text" Value="Mouse Left BUtton Down"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBlock>

如果您知道为什么会发生这种情况,请启发我...如果有其他/更好的方法来执行此操作,除了状态之外,请告诉我。

谢谢。

最佳答案

它失败了,因为 Button 上的 Content 属性不仅仅是一个字符串,而是一个对象。尽管 Blend 会让你设置它,但我不确定它是否可行。要更改按钮的文本内容,您需要执行以下操作:

<Button x:Name="button" HorizontalAlignment="Left" Height="23" Margin="154,98,0,0" VerticalAlignment="Top" Width="113">
    <Button.Content>
        <TextBlock x:Name="btnText" Text="Orginal"  />
    </Button.Content>
    <i:Interaction.Triggers>
    <i:EventTrigger>
      <ei:ChangePropertyAction PropertyName="Text" Value="Loaded" TargetName="btnText"/>
    </i:EventTrigger>
    <i:EventTrigger EventName="Click">
        <ei:ChangePropertyAction PropertyName="Text" Value="Clicked" TargetName="btnText"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

关于Button 控件上的 Silverlight ChangePropertyAction 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9660305/

相关文章:

c# - 在Silverlight 5,MVVM中显示鼠标位置

wpf - Clean Implementation 在 MVVM ViewModel 中显示弹出错误消息

silverlight - 在Silverlight中,什么是FlowDocument的最佳替代品?

c# - 导航时 Silverlight NavigationFramework 不更改地址栏(不显示主题标签)

c# - WPF:一些 Expression Blend 主题导致 Combobox 绑定(bind)/显示中的错误

silverlight - 如何在 MVVM 中的模块/ View 之间制作动画?

silverlight - Silverlight 4 和 Windows Phone 7 的 Bing map 控件中的交互式图层

wpf - 如何绑定(bind)文本框和属性?

silverlight - 在 XAML 中访问 WebContext 类

mvvm - 如何使用 Blend 在 WP7 中以不同的视觉状态设置不同的本地化字符串?