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

Windows phone 8 如何在Windows Phone 8中更改列表框内的按钮背景图像,windows-phone-8,Windows Phone 8,大家好,当在列表框中使用按钮样式按下按钮时,如何更改按钮背景图像 </Grid.ColumnDefinitions> <StackPanel Orientation="Horizontal" Margin="0, 10, 0, 10"> <TextBlock x:Name="item_name" Text="{B

大家好,当在列表框中使用按钮样式按下按钮时,如何更改按钮背景图像

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

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

我不确定您想要什么,因此首先,您可以尝试以下方法:

                        </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;
或者查看此链接:


                        </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>
代码隐藏:
Button obj=发送方作为按钮;
ImageBrush ib=新的ImageBrush();
ib.ImageSource=新的位图图像(新的Uri(@“ms-appx:///cake-icon_64x64.png"));
对象背景=ib

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

这将加载带有“Logo.png”图像的列表项,并单击从代码隐藏更改为“cake icon.png”。

显示以下错误资源“ListBoxDT”无法解析“ListBoxDT”是数据模板的名称。请仔细阅读代码。谢谢Nishi,它工作正常。但我想显示两个状态图像按钮,例如开/关(切换类型)。请将其标记为工作答案。关于两国形象。您可以有一个bool属性/变量来维护按钮状态,您可以定义该属性/变量(可能最初为true,然后在后续单击时为false,然后在下次单击时再次为true)。点击按钮,您可以检查此属性的值并相应地设置图像。Hi Nishi,请检查另一个问题
                        </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>