繁体   English   中英

如何访问控件,该控件位于ControlTemplate中

[英]How to access controls, which are in the ControlTemplate

在我的WP8应用程序的App.xaml文件中,我定义了ControlTemplate如下:

<Application.Resources>
    <ControlTemplate x:Name="AddReminderDialog">
            <Canvas  HorizontalAlignment="Center"  Height="320" Width="260"
                VerticalAlignment="Center" Background="White" Margin="110,178,110,238">
                <TextBlock Foreground="Black" Text="Напомнить" FontSize="15" HorizontalAlignment="Center" Canvas.Left="92" Canvas.Top="38" />
                <Button Name="btn1HourBef" BorderThickness="0" Click="NotificationButtonClick" Background="Black" Content="За час" Width="260" FontSize="15" Height="60" Margin="0,70,0,0"/>
                <Button Name="btn30MinBef" BorderThickness="0" Click="NotificationButtonClick" Background="Black" Content="За 30 минут" Width="260" FontSize="15" Height="60" Margin="0,130,0,0"/>
                <Button Name="btnOnArrDept" BorderThickness="0" Click="NotificationButtonClick" Background="Black" Content="По прилету/вылету" Width="260" FontSize="15" Height="60" Margin="0,190,0,0"/>
                <Button Name="btnCancel" BorderThickness="0" Click="NotificationButtonClick" Background="Black" Content="Отменить" Width="260" FontSize="15" Height="60" Margin="0, 250, 0, 0" Visibility="Collapsed"/>
        </Canvas>
    </ControlTemplate>
</Application.Resources>

我将其用作弹出模板,如下所示

private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    var image = sender as Image; //get the sender iamge
    var modelItem = image.DataContext; //get image's data context

    const double width = 260;
    const double height = 280;

    //get the flight id from image's tag property(which was binded in flightInfoDataTemplate)
    flightID = Convert.ToString(image.Tag);

    //define content for popup
    var content = new ContentControl()
    {
        Width = width,
        Height = height,
        Background = new SolidColorBrush(Colors.Transparent)
    };

    //set the template of content to the contentTemplate, which was defined in app.xaml
    content.Template = (ControlTemplate)Resources["AddReminderDialog"];


    //set popup's datacontext to the image's datacontext
    content.DataContext = modelItem;

    //popup's child property is setting to our content
    popup.Child = content;

    popup.Height = height;
    popup.Width = width;
    popup.VerticalOffset = Application.Current.RootVisual.RenderSize.Height / 2 - height / 2;
    popup.HorizontalOffset = Application.Current.RootVisual.RenderSize.Width / 2 - width / 2;
    popup.IsOpen = true;
}

我使用此弹出窗口设置通知。 当用户第一次点击图像时,btnCancel按钮应该是不可见的,因为没有要取消的内容。 当第二次点击图像时,btnCancel应该变得可见以取消通知。 我已将按钮可见性默认设置为折叠。 但是我不知道如何在后面的代码中访问该按钮以使其可见。 所以我的问题是如何在后面的代码中更改按钮的可见性设置?

你可以做这样的事情,

   private void SearchElement(DependencyObject targeted_control) 
      {           
      var count = VisualTreeHelper.GetChildrenCount(targeted_control);   // targeted_control is the Canvas  
      if (count > 0)
        {
        for (int i = 0; i < count; i++)
          {
          var child = VisualTreeHelper.GetChild(targeted_control, i);
          if (child is Button ) // specific button control 
            {
             // do your logic
            }
          }
       }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM