如何在Windows Phone 8中更改列表框内的按钮背景图像

时间:2015-05-16 09:40:56

标签: windows-phone-8

大家如何使用按钮样式按下列表框中的按钮

时更改按钮背景图像

                                                                                                                            

                        </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>

2 个答案:

答案 0 :(得分:0)

我不确定你想要的是什么,你可以试试这个:

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

或查看此链接:WPF Change button background image when clicked

答案 1 :(得分: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>

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

这会使用&#34; Logo.png&#34;加载列表项图片和点击更改为&#34; cake-icon.png&#34;从代码背后。