以编程方式更改Windows Phone中按钮的背景

时间:2014-07-16 11:19:34

标签: xaml windows-phone-8 windows-phone

在这种情况下,如何以编程方式更改背景?

        <Button x:Name="ButtonPlayBack1" Tag="0" Grid.Column="1" Height="60" Width="70" Click="ButtonPlayBack_OnClick">
        <Button.Template>
            <ControlTemplate>
                <Grid>
                    <Grid.Background>
                        <ImageBrush ImageSource="/Assets/images/bubble_right_grey.png"/>
                    </Grid.Background>
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>

2 个答案:

答案 0 :(得分:2)

试试这个:

XAML:

        <Button x:Name="ButtonPlayBack1" Tag="0" Grid.Column="1" Height="60" Width="70" Click="ButtonPlayBack1_Click" BorderThickness="0">
            <Button.Background>
                <ImageBrush ImageSource="/Assets/images/bubble_right_grey.png"></ImageBrush>
            </Button.Background>
        </Button>

CS:

private void ButtonPlayBack1_Click(object sender, RoutedEventArgs e)
{
    BitmapImage bit = new BitmapImage(new Uri("/Assets/AlignmentGrid.png", UriKind.Relative));
    ImageBrush brush = new ImageBrush();
    brush.ImageSource = bit;
    ButtonPlayBack1.Background = brush;
}

答案 1 :(得分:0)

解决方案:

Grid grid = ButtonPlayBack1.GetLogicalChildrenByType<Grid>(true).First();

if (grid != null)
grid.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri(backGroundSrc, UriKind.Relative)) };