如何在 Windows Phone 8 中更改列表框内的按钮背景图像
How to Change Button Background Image inside Listbox In 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>
我不确定你想要什么,所以你可以试试这个:
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(@"Images/myImage.png", UriKind.Relative));
myButton.Background = brush;
或者看这个 link : WPF Change button background image when clicked
<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;
这会加载带有 "Logo.png" 图片的列表项,并在点击后从代码隐藏更改为 "cake-icon.png"。
大家好,当在列表框中按下按钮时,我如何使用按钮样式更改按钮背景图像
</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>
我不确定你想要什么,所以你可以试试这个:
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(@"Images/myImage.png", UriKind.Relative));
myButton.Background = brush;
或者看这个 link : WPF Change button background image when clicked
<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;
这会加载带有 "Logo.png" 图片的列表项,并在点击后从代码隐藏更改为 "cake-icon.png"。