Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Button with regular image and disabled image

 
 
Old 05-23-2006   #1 (permalink)
loumfranco@gmail.com


 
 

Button with regular image and disabled image

I am trying to make a button in xaml with different images when enabled
and disabled. I tried using a Trigger on IsEnabled, but when I tried
to set the Content to an image, I got an exception

'System.Windows.Controls.Image' value not valid for property 'Content'.
'Setter' does not support values derived from Visual or ContentElement.

The XAML is this

<Style x:Key="undoStyle" TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Content">
<Setter.Value>
<Image Source="Images/Undo.png" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Content">
<Setter.Value>
<Image Source="Images/Undo-Disabled.png" />
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>


I started down the road of using a DataTemplate to style an object (was
going to use an XML data island). Haven't gotten that to work, but
this all seems pretty complicated for such a simple thing. I am about
to do it in C#, but in case I'm missing something obvious, I thought
I'd ask.


My System SpecsSystem Spec
Old 05-31-2006   #2 (permalink)
Shalini Aggarwal [MSFT]


 
 

RE: Button with regular image and disabled image

Hi,

Attempting to set a button's content to a Visual or ContentElement in a
Style will result in the error you note -- Styles can be shared, whereas the
Visual/ContentElement you are trying to put inside it can not be shared. Let
me know if you would like further explanation on this.

To accomplish what I think you are trying to do here, you could use a
DataTemplate to set your Button's ContentTemplate as follows (using XAML):

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Page.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Image Source="go.png" Width="100" x:Name="theImage"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type
Button}}, Path=IsEnabled}" Value="False">
<Setter TargetName="theImage" Property="Source"
Value="disable.png"/>

</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>

<StackPanel>
<Button IsEnabled="False"/>
<Button />
</StackPanel>
</Page>

-Shalini

----------
"loumfranco@gmail.com" wrote:

> I am trying to make a button in xaml with different images when enabled
> and disabled. I tried using a Trigger on IsEnabled, but when I tried
> to set the Content to an image, I got an exception
>
> 'System.Windows.Controls.Image' value not valid for property 'Content'.
> 'Setter' does not support values derived from Visual or ContentElement.
>
> The XAML is this
>
> <Style x:Key="undoStyle" TargetType="{x:Type Button}">
> <Style.Triggers>
> <Trigger Property="IsEnabled" Value="True">
> <Setter Property="Content">
> <Setter.Value>
> <Image Source="Images/Undo.png" />
> </Setter.Value>
> </Setter>
> </Trigger>
> <Trigger Property="IsEnabled" Value="False">
> <Setter Property="Content">
> <Setter.Value>
> <Image Source="Images/Undo-Disabled.png" />
> </Setter.Value>
> </Setter>
> </Trigger>
> </Style.Triggers>
> </Style>
>
>
> I started down the road of using a DataTemplate to style an object (was
> going to use an XML data island). Haven't gotten that to work, but
> this all seems pretty complicated for such a simple thing. I am about
> to do it in C#, but in case I'm missing something obvious, I thought
> I'd ask.
>
>

My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Datagrid Image Button Select Column .NET General
Various Errors - winlogon.exe Bad Image, Windows - Bad Image & Window Defender General Discussion
Acronis True Image recovery of Vista Image Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46