views:

17

answers:

2

I have a ListBox in Silverlight that has a list of items inside. Each item has a certain number of additional options, the availability of which depends on the each item.

   <ListBox.ItemTemplate>
      <DataTemplate>
         <StackPanel Orientation="Horizontal">                                    
            <TextBlock Text="{Binding Name}" />
            <Button HorizontalAlignment="Right" x:Name="editDiarySecurityButton">
               <Image Source="/xxx.yyy.Silverlight.Common;Component/Resources/Images/Icons/Ribbon/Small/editSecurity.png" Width="16" Height="16" />
            </Button>
         </StackPanel>
      </DataTemplate>                                                           
   </ListBox.ItemTemplate>  
</ListBox>

The Button editDiarySecurityButton should be handled according to whether that item (representing a Diary) has Security applied to it, or not. I would probably just modify the opacity of the image to reflect this.

My question is how can I achieve this? In ASP.NET, I'd have attached to the ItemDataItemBound event, but I don't think this is available in WPF/Silverlight.

+1  A: 

If I understand what you're asking, you'll want to expose a property on the object and use that to present your elements as necessary. ie to disable the button you might use something like IsEnabled="{Binding HasSecurity}".

Dan Wray
+1  A: 

Since you asked specifically about the Opacity property, bind the Button's Opacity property to a property on your DataContext that returns a double. Add whatever logic you need to the property to return the Opacity you desire for that item. Something like this:

<Button HorizontalAlignment="Right" x:Name="editDiarySecurityButton"
 Opacity="{Binding Path=ButtonOpacity, Mode=OneWay}"> 
    <Image Source="/xxx.yyy.Silverlight.Common;Component/Resources/Images/Icons/Ribbon/Small/editSecurity.png" Width="16" Height="16" /> 
</Button> 

Then on your DataContext:

public double ButtonOpacity
{
    get {return _buttonOpacity; }
}

Use the same idea with any other property of the Button you want to control.

DaveB
Thanks. You know, I'm still getting into this WPF lark. I never actually thought of it this way. Guess the best way would be to sub-class my data. Though as my class is a DTO from EF/WCF, I guess I may just have to wrap it. Unfortunately, I can only choose one answer!
Program.X