tags:

views:

155

answers:

2

I have a listview in a WPF application that has a button in one of its columns. I associate a image to the button control. I want to configure different images for this button based on the How do I write the XAML in this scenario???

This is what I have already:

<ControlTemplate x:Key="IconButtonAddProfile" TargetType="{x:Type Button}">
  <Grid>
    <Image x:Name="myimage" Source="rofile.png" Height="27" Width="65" />
    <Border>
      <ContentPresenter Content="{TemplateBinding Content}"/>
    </Border>
  </Grid>
</ControlTemplate>

<ListView.View>
  <GridView>
    <GridViewColumn Header="Security" Width="50">
      <GridViewColumn.CellTemplate>
        <DataTemplate>
          <Button Template="{StaticResource IconButtonLockSecure}"
                  DataContext="{Binding}" MinHeight="20" MinWidth="50" />
        </DataTemplate>
      </GridViewColumn.CellTemplate>
    </GridViewColumn>
</ListView.View>
A: 

Let's assume that your model has a property "ButtonImage" that returns the ImageSource of the image for the object, and a property "ButtonContent" that returns the content of the button (text or whatever).

Then your ControlTemplate can be:

<ControlTemplate x:Key="IconButtonAddProfile" TargetType="{x:Type Button}"> 
  <Grid> 
    <Image x:Name="myimage" Source="{Binding ButtonImage}" /> 
    <Border> 
      <ContentPresenter Content="{Binding ButtonContent}"/> 
    </Border> 
  </Grid> 
</ControlTemplate> 

Note that you also don't need to set DataContext on your Button, since it is inherited.

Ray Burns
A: 

I did add a property ButtonImage that returned a hardcoded image path. public string ButtonImage { get { return @"Images/glowLine.png"; } } I configured the ListView column as follows.

                <GridViewColumn Header="Add Profile">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button  Template="{Binding IconButtonAddProfile}" Click="Button_Click" DataContext="{Binding}" MinHeight="20" MinWidth="50" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

But the image does not show up in the listview

Narayanan M