1

Hi all how i can change button background image when button is pressed inside listbox using style for button

                        </Grid.ColumnDefinitions>
                        <StackPanel Orientation="Horizontal" Margin="0, 10, 0, 10">


                            <TextBlock x:Name="item_name" Text="{Binding Name, Mode=OneWay}" FontSize="{StaticResource PhoneFontSizeLarge}" Margin="10, 0, 0, 0" />


                        </StackPanel>
                      <Border BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" Margin="5, 0, 0, 0">
                            <Button x:Name="btn_pic1" Style="{StaticResource ImageButton}"  Content="Hide" BorderThickness="0" Visibility="Visible" Click="btn_pic1_Click"  Width="100" Height="60" Margin="345, 0, 0, 0" >
                                <Button.Background>
                                    <ImageBrush ImageSource="/Assets/Images/green.circle.png" Stretch="Fill"/>
                                </Button.Background>
            </Button>
                        </Border>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
user
  • 125
  • 8

2 Answers2

0

I'm not sure of what you want so first , you can try this :

var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(@"Images/myImage.png", UriKind.Relative)); 
myButton.Background = brush;

or look this link : WPF Change button background image when clicked

Community
  • 1
  • 1
Fly_federer
  • 206
  • 1
  • 2
  • 12
0
    <Style TargetType="Button" x:Key="ImageButton">
        <Setter Property="Foreground" Value="Red" />
    </Style>

    <DataTemplate x:Name="ListBoxDT">
        <Grid Margin="10,15,10,0">
                     <Border BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" Margin="5, 0, 0, 0">
                <Button x:Name="btn_pic1" Style="{StaticResource ImageButton}"  Content="Hide" BorderThickness="0" Visibility="Visible" Click="btn_pic1_Click"  Width="100" Height="60" Margin="0, 0, 0, 0" >
                    <Button.Background>
                        <ImageBrush ImageSource="Assets/Logo.png" Stretch="Fill"/>
                    </Button.Background>
                </Button>
            </Border>
        </Grid>
    </DataTemplate>

<Grid>
    <ListBox Name="list" ItemTemplate="{StaticResource ListBoxDT}">
    </ListBox>
</Grid>

code behind: Button obj = sender as Button; ImageBrush ib = new ImageBrush(); ib.ImageSource = new BitmapImage(new Uri(@"ms-appx:///cake-icon_64x64.png")); obj.Background = ib;

This loads the list item with "Logo.png" image and on click changes to "cake-icon.png" from code behind.

Nishi
  • 614
  • 4
  • 18
  • showing following error The resource "ListBoxDT" could not be resolved – user May 19 '15 at 13:03
  • "ListBoxDT" is the name of the DataTemplate. see the code carefully. – Nishi May 19 '15 at 14:31
  • Thanks Nishi its working fine . but i want to show two state image button for example On/Off (Toggle type). – user May 20 '15 at 04:37
  • please mark it as working answer. Regarding two state image. you can have a bool property/variable to maintain button state, which you can define(may be initially true, then on subsequent click to false and then true again on next click). and on button click you can check the value of this property and set the image accordingly. – Nishi May 20 '15 at 05:21
  • Hi Nishi Please check another issue http://stackoverflow.com/questions/30092887/how-to-save-checkboxes-state-in-listbox-windows-phone-8 – user May 20 '15 at 05:28