C# Windows Phone 8.1 Siverlight中的OnApplyTemplate?
我使用模板更改VisualStudio中默认按钮的外观。我正在绘制一个多边形,并使用ContentPresenter标记在多边形中插入文本C# Windows Phone 8.1 Siverlight中的OnApplyTemplate?,c#,silverlight,windows-phone-8.1,C#,Silverlight,Windows Phone 8.1,我使用模板更改VisualStudio中默认按钮的外观。我正在绘制一个多边形,并使用ContentPresenter标记在多边形中插入文本 <ControlTemplate x:Key="AnswerChoose" TargetType="Button"> <Border BorderThickness="2"> <Grid Name="ButtonAnswer"> <Polygon
<ControlTemplate x:Key="AnswerChoose" TargetType="Button">
<Border BorderThickness="2">
<Grid Name="ButtonAnswer">
<Polygon Name="BorderAnswer" Points="0,50 30,20, 270,20 300,50 270,80 30,80" Stroke="White" StrokeLineJoin="Bevel" StrokeThickness="2">
<Polygon.Fill>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="#FF0A080D" Offset="0.0"/>
<GradientStop Color="#FF15224C" Offset="0.2"/>
<GradientStop Offset="0.5" Color="#FF0460C3"/>
<GradientStop Color="#FF15224C" Offset="0.8"/>
<GradientStop Color="#FF0A080D" Offset="1"/>
</LinearGradientBrush>
</Polygon.Fill>
</Polygon>
<ContentPresenter Name="ContentAnswer" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</Border>
</ControlTemplate>
这是我的按钮:
<Grid Grid.Row="2" HorizontalAlignment="Stretch" Tap="ChangeColorButton_Tap">
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Button Name="A" Content="A." Grid.Column="0" Grid.Row="0" Template="{StaticResource AnswerChoose}" />
<Button Name="B" Content="B. " Grid.Column="1" Grid.Row="0" Template="{StaticResource AnswerChoose}"/>
<Button Name="C" Content="C. " Grid.Column="0" Grid.Row="1" Template="{StaticResource AnswerChoose}"/>
<Button Name="D" Content="D. " Grid.Column="1" Grid.Row="1" Template="{StaticResource AnswerChoose}"/>
</Grid>
</Grid>
现在,我想如果我点击那个按钮,它会改变颜色,但我有一个问题:如果我点击节点和边缘属性OriginalSource返回边框,如果我点击按钮的文本,它返回TextBlock。如何知道VisualStudio单击按钮上的任何位置是否会返回该按钮。我在MSDN上有参考资料,但它只在应用程序模板使用上含糊不清。您能告诉我如何使用它或提供其他解决方案吗?如果您不想为每个按钮添加事件处理程序,您可以尝试使用VisualTreeHelper搜索元素 在ChangeColorButton\u Tap事件处理程序中尝试以下代码:
var pos = e.GetPosition(null);
var buttons = VisualTreeHelper.FindElementsInHostCoordinates(pos, LayoutRoot).Where(item => item is Button);
foreach (var uiElement in buttons)
{
Debug.WriteLine(uiElement);
}