0

我找不到在主页资源和样式中定义的按钮。在访问该按钮后,我想将其视觉状态更改为按下。这是我在代码隐藏(MainPage.xaml.cs)中尝试的:

private void OnShowPopupButtonClick(object sender, RoutedEventArgs e)
    {
        this.myPopup.IsOpen = !this.myPopup.IsOpen;

        //attempt at accessing button resource to change state
        if ((this.myPopup.IsOpen))
        {
            this.HomeButton.Controls.Style = this.Resources["PivotStyle1"] as Style;
            VisualStateManager.GoToState(HomeButton, "Pressed", true);
        }
    }

HomeButton 所在的层次结构(在 MainPage.xaml 中)如下:

    <phone:PhoneApplicationPage.Resources>
         ...
         <Style x:Key="PivotStyle1" TargetType="controls:Pivot">
              ....
              <Setter Property="Template">
              ...
            <Setter.Value>
                <ControlTemplate TargetType="controls:Pivot">
                ...
                <Grid>
                    <ContentPresenter>
                        <StackPanel>
                            <StackPanel>
                                <Button x:Name="HomeButton" Style="{StaticResource ButtonStyle1}" Click="OnShowPopupButtonClick" >
.....
.....

这里是我在 layoutroot 中调用我的风格的地方:

    <Grid x:Name="LayoutRoot">
        <controls:Pivot x:Name="MainPivotControl" Title="MyApp" Style="{StaticResource PivotStyle1}" FontFamily="myFont">
...
...

因此,当我尝试构建应用程序时,会弹出一条错误消息,提示 HomeButton 在当前上下文中不存在。另一个错误消息说它没有定义或扩展方法。有谁知道我在后面的代码中可能做错了什么?这几天我一直在浏览网页并浏览一些教程,但没有任何帮助。谢谢


编辑:这是用户“nit”建议的代码:但断点显示 HomeButton 仍未找到。

private void OnShowPopupButtonClick(object sender, RoutedEventArgs e)
    {
        this.myPopup.IsOpen = !this.myPopup.IsOpen;

        //attempt at accessing button resource to change state
        if ((this.myPopup.IsOpen))
        {
            //This section will help locate and assign HomeButton then event function will launch
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(MainPivotControl); i++)
            {
                var child = VisualTreeHelper.GetChild(MainPivotControl, i);
                if (child is Button)
                {
                    var myHomeButton = child as Button;
                    //This is where the event procedure will be given
                    if (myHomeButton.Name == "HomeButton")
                    {
                        VisualStateManager.GoToState(myHomeButton, "Pressed", true);
                        break;
                    }

                }
            }

        }
        else
        {
            //This section will help locate and assign HomeButton then event function will launch
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(MainPivotControl); i++)
            {
                var child = VisualTreeHelper.GetChild(MainPivotControl, i);
                if (child is Button)
                {
                    var myHomeButton = child as Button;
                    //This is where the event procedure will be given
                    if (myHomeButton.Name == "HomeButton")
                    {
                        VisualStateManager.GoToState(myHomeButton, "Pressed", false);
                        break;
                    }

                }
            }
        }

    }
4

2 回答 2

0

您不能像这样访问您的 HomeButton,因为它不是直接为您的窗口类定义的变量。为了访问它,您必须在 Pivot 控件的可视树中搜索它,如下所示:

 public Button FindButton(DependencyObject parent)
 {
    // Confirm parent and childName are valid. 
    if (parent == null) return null;

    Button homeButton = null;

    int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < childrenCount; i++)
    {
        var child = VisualTreeHelper.GetChild(parent, i);
        // If the child is not of the request child type child
        Button button = child as Button;
        if (button == null)
        {
            // recursively drill down the tree
            homeButton = FindButton(child);

            // If the child is found, break so we do not overwrite the found child. 
            if (homeButton != null) break;
        }
        else
        {
            // If the child's name is set for search
            if (button.Name == "HomeButton")
            {
                // if the child's name is of the request name
                homeButton = button;
                break;
            }
        }
    }

    return homeButton;
}
于 2013-09-15T14:10:00.917 回答
0

您可以在按钮上添加 Loaded 事件,并将 sender 参数保存在将成为您的按钮的字段中。

//button can be reused everywhere (including inside your OnShowPopupButtonClick function)
Button myPopupButton;

public myPopupButton_Loaded(object sender, RoutedEventArgs e) { 
    myPopupButton=sender as Button;
    if ((this.myPopup.IsOpen))
    {
        VisualStateManager.GoToState(myPopupButton, "Pressed", true);
    }
} 
于 2013-09-15T14:17:14.870 回答