0

我正在制作一种清单应用程序。StackPanel 中有一系列 TextBlocks,当点击调用时:

public void TextBlock1(object sender, MouseButtonEventArgs e)
    {
        (App.Current as App).detailTitle = "Check 1";
        (App.Current as App).detailMessage = "This is item 1";
        (App.Current as App).pageReferrer = "pivot1.Check1";
        Uri detailPage = new Uri("/DetailPage.xaml", UriKind.Relative);
        NavigationService.Navigate(detailPage);
    }

然后在 detailPage.xaml 上:

public DetailPage()
    {
        InitializeComponent();
        DetailPageTitle.Text = (App.Current as App).detailTitle;
        textBlockDetail.Text = (App.Current as App).detailMessage;
    }

    private void doneButton_Click(object sender, EventArgs e)
    {
        NavigationService.GoBack();
    }

现在,当 doneButton_Click 发生时,我希望将引用的 TextBlock 样式从 PhoneTextExtraLargeStyle 更改为我创建的名为 TextBlockStyleClicked 的样式。

我怎样才能做到这一点?

4

1 回答 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];
    }
}

使用:

ApplicationTitle.Style = (Style) ApplicationTitle.TryFindResource("PhoneTextTitle1Style");

希望这可以帮助。

于 2013-09-28T20:37:58.573 回答