【发布时间】:2016-04-18 17:18:05
【问题描述】:
这似乎不是什么大问题,但无论出于何种原因,我在 XAML 中创建了一个简单的矩形,现在无法引用它以在运行时以编程方式更改它。它应该做的是检查是否已选择国家面板之一,如果所选国家的字段之一与中心部分的名称匹配,它将突出显示中心部分底部的矩形以指示选择中心部分的用户。我可以很好地引用集线器部分本身,但我无法访问矩形的子对象。首先是其中一个中心部分的 XAML:
<HubSection x:Name="China" Width="440" Height="460" Background="#FF343434" Header="China" IsHeaderInteractive="True" Tapped="{x:Bind HubSectionTapped}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="50,0,0,0">
<DataTemplate>
<Grid Height="460" Width="410" VerticalAlignment="Bottom" x:Name="ChinaBackground">
<Image Source="Assets/chinaFlag.bmp" x:Name="ChinaFlag"/>
<Rectangle x:Name="ChinaSelected_Rect" Width="410" Height="30" VerticalAlignment="Bottom" Fill="BlueViolet" Opacity="0"/>
</Grid>
</DataTemplate>
</HubSection>
这是它背后的代码:
private void Timer_SelectionIndicator(object sender, object e)
{
if (currentCountry1 == "China" || currentCountry2 == "China")
{
//this line is the one that throws an error:
//it says it needs an object reference to the rectangle, but...
//I don't need a reference to reference the hub section itself... why?
ChinaSelected_Rect.Opacity = 100;
}
}
【问题讨论】:
-
不确定 XAML,但在 Web 表单中,如果您有绑定控件,则必须从容器中执行 FindControl 以获取对模板中控件的引用。
标签: c# xaml reference controls win-universal-app