单击按钮时WP8更改背景图像

时间:2014-06-23 20:02:29

标签: c# xaml windows-phone-8 background-image

单击按钮时,我想更改主页中的背景图像。在xaml我有; Style="{StaticResource LayoutGridStyle}为背景。我怎样才能做到这一点?谢谢!

2 个答案:

答案 0 :(得分:0)

将样式或背景图像(但是您想要处理它)作为主页的ViewModel中的属性包含在内。然后绑定到这个......

Style="{Binding NameOFViewModelProperty}

当您通过按钮单击代码更改样式时,请确保引发属性更改事件...

RaisePropertyChanged("NameOFViewModelProperty");

因此页面知道用更改刷新自己。

答案 1 :(得分:0)

可以这样做:

public static class FrameworkElementExtensions
    {
        public static object TryFindResource(this FrameworkElement element, object resourceKey)
        {
            var currentElement = element;

            while (currentElement != null)
            {
                var resource = currentElement.Resources[resourceKey];
                if (resource != null)
                {
                    return resource;
                }

                currentElement = currentElement.Parent as FrameworkElement;
            }

            return Application.Current.Resources[resourceKey];
        }
    }

        private void PageTitle_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            ApplicationTitle.Style = (Style)ApplicationTitle.TryFindResource("PhoneTextTitle1Style");
        }

示例为 here