Search code examples
silverlightsilverlight-4.0expression-blendsilverlight-5.0

Silverlight ChangePropertyAction on Button control not working


Out of the box if you crank up Blend put a button on a grid and set a couple of ChangeProeprtyActions on it ... will fail with an error message.

<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>

The error message is this:

    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

If you add a ChangePropertyAction on a TextBlock ... works fine:

<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>

If you have any idea why this happens please enlighten me ... if there is another/better way of doing this, other than states please let me know.

Thanks.


Solution

  • It is failing because the Content property on a Button is not just a string but an object. Even though Blend will let you set it I'm not sure that it is possible. To change the text content of the button you would need to do something like this:

    <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>