Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to figure out how this would be possible to accomplish;

I have a button, if the ImageSource property of a viewmodel is not null I would like to add an image to this button. if ImageSource is equal to null I would bind the content of the button to "DisplayName" property of the same viewmodel.

Would this be possible ?
Posted

I think it could be done with a setter trigger.

Make a trigger on IsImageSourceNull and create that property in the ViewModel. If true set button content to DisplayName if false set it to the image.
 
Share this answer
 
Comments
Frode Jensen 16-Sep-10 4:51am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Hi,

I dont know whether your design is suited the below impementation.Try the following.

create a data template as follows
XML
<DataTemplate x:Key="MyButtonContentTemplate" DataType="{x:Type local:YourViewModelObject}">
           <Grid>
               <TextBlock x:Name="txt" Text="{Binding display_name}"/>
               <Image x:Name="img" Source="{Binding image_source}"/>
           </Grid>
           <DataTemplate.Triggers>
               <DataTrigger Binding="{Binding image_source}" Value="{x:Null}">
                   <Setter Property="Visibility" Value="Collapsed" TargetName="img"/>
               </DataTrigger>
               <DataTrigger Binding="{Binding display_name}" Value="{x:Null}">
                   <Setter Property="Visibility" Value="Collapsed" TargetName="txt"/>
               </DataTrigger>
           </DataTemplate.Triggers>
       </DataTemplate>


then set the content as your view model object and apply the content template as the above template.

<Button ContentTemplate="{StaticResource MyButtonContentTemplate}" x:Name="btn" Width="100" Height="50"/>


btn.Content = YourViewModelObject;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900