C# 如何访问ControlTemplate中的控件
在我的WP8应用程序的App.xaml文件中,我将ControlTemplate定义如下:C# 如何访问ControlTemplate中的控件,c#,windows-phone-8,C#,Windows Phone 8,在我的WP8应用程序的App.xaml文件中,我将ControlTemplate定义如下: <Application.Resources> <ControlTemplate x:Name="AddReminderDialog"> <Canvas HorizontalAlignment="Center" Height="320" Width="260" VerticalAlignment="Cente
<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>
我使用此弹出窗口设置通知。当用户第一次点击图像时,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
}
}
}
}
private void SearchElement(DependencyObject-targeted\u控件)
{
var count=VisualTreeHelper.GetChildrenCount(目标_控件);//目标_控件是画布
如果(计数>0)
{
for(int i=0;i
在我将弹出窗口的可见性更改为可见之前,它会工作吗?在弹出窗口显示给用户之前,我必须改变按钮的可见性。为什么不试试呢?@BenjaminPaul我已经用弹出窗口尝试过这种方法,我的意思是我已经将弹出窗口作为参数发送到该方法,并检查了计数。计数等于0。所以它不起作用了我的画布位于应用程序资源中的controlTemplate中。所以我无法将画布发送到该方法,这就是我发送弹出窗口的原因。
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
}
}
}
}