在WPF中加载XAML后访问UIElement
本文关键字:访问 UIElement XAML 加载 WPF | 更新日期: 2023-09-27 17:57:44
我有一个Canvas
,它是使用从文件的XAML加载中填充的
SBWindowContainerCanvas.Children.Clear();
StreamReader stringreader = new StreamReader("C:''xaml''xmltest.xaml");
XmlReader xmlreader = XmlReader.Create(stringreader);
var mainborder = XamlReader.Load(xmlreader);
SBWindowContainerCanvas.Children.Add((Blacklight.Controls.ClippingBorder)mainborder);
边界包含许多子元素,包括一些用户控件。
XAML文件的部分片段:
<av:ContentControl Name="VisScoreCC" Width="417" Height="228" IsHitTestVisible="True" av:Canvas.Left="855" av:Canvas.Top="8" av:Selector.IsSelected="False">
<fsp:Scores3DigitControl Name="Vis3DigitScoreControl" VisitorControl="True" OperatorControl="True" NumericValue="4" UseGradientMainBG="False" UseGradientNumberBG="False" UseGradientLogoBG="False" UseGradientTeamNameBG="False" UseRadialMainBG="False" UseRadialNumberBG="False" UseRadialLogoBG="False" UseRadialTeamNameBG="False" UseImageStringMainBG="False|none|False|Tile|Top Left|1" SolidColorMainBG="#FFD4D2D2" SolidColorNumberBG="#FF000000" SolidColorBannerBG="#FF000000" FGColorNumbers="#FFFF0000" FGColorTeamName="#FFFFFFFF" FGColorBanner="#FFFFA500" SolidColorImageLogoBG="#FF000000" SolidColorTeamNameBG="#FF000000" GradientMainBG="#FF000000|#FF808080|#FFFFFFFF" GradientNumberBG="#FF000000|#FF808080|#FFFFFFFF" GradientLogoBG="#FF000000|#FF808080|#FFFFFFFF" GradientTeamNameBG="#FF000000|#FF808080|#FFFFFFFF" GradientOffsetsMainBG="0|0.5|1" GradientOffsetsNumbersBG="0|0.5|1" GradientOffsetsLogoBG="0|0.5|1" GradientOffsetsTeamNameBG="0|0.5|1" LinearAngleMainBG="180" LinearAngleNumberBG="180" LinearAngleLogoBG="180" LinearAngleTeamNameBG="180" OffSegmentOpacity="0.1" RoundRadiusNumbers="0" RoundRadiusLogo="0" RoundRadiusTeamName="0" PosSizeStringNumbers="180|8|200|150" PosSizeStringLogo="20|10|150|150" PosSizeStringTeamName="25|165|360|60" TeamNameFontString="Arial|40|True" UseRightSideBanner="True" Style="{av:DynamicResource Scores3DigitControlTemplate}" Height="Auto" Margin="0,0,0,0" IsHitTestVisible="True" />
</av:ContentControl>
<av:ContentControl Name="HomeScoreCC" Width="417" Height="228" IsHitTestVisible="True" av:Canvas.Left="11" av:Canvas.Top="8" av:Selector.IsSelected="False">
<fsp:Scores3DigitControl Name="Home3DigitScoreControl" VisitorControl="False" OperatorControl="True" NumericValue="4" UseGradientMainBG="False" UseGradientNumberBG="False" UseGradientLogoBG="False" UseGradientTeamNameBG="False" UseRadialMainBG="False" UseRadialNumberBG="False" UseRadialLogoBG="False" UseRadialTeamNameBG="False" UseImageStringMainBG="False|none|False|Tile|Top Left|1" TargetElementIndex="0" SolidColorMainBG="#FFD4D2D2" SolidColorNumberBG="#FF000000" SolidColorBannerBG="#FF000000" FGColorNumbers="#FFFF0000" FGColorTeamName="#FFFFFFFF" FGColorBanner="#FFFFA500" SolidColorImageLogoBG="#FF000000" SolidColorTeamNameBG="#FF000000" GradientMainBG="#FF000000|#FF808080|#FFFFFFFF" GradientNumberBG="#FF000000|#FF808080|#FFFFFFFF" GradientLogoBG="#FF000000|#FF808080|#FFFFFFFF" GradientTeamNameBG="#FF000000|#FF808080|#FFFFFFFF" GradientOffsetsMainBG="0|0.5|1" GradientOffsetsNumbersBG="0|0.5|1" GradientOffsetsLogoBG="0|0.5|1" GradientOffsetsTeamNameBG="0|0.5|1" LinearAngleMainBG="180" LinearAngleNumberBG="180" LinearAngleLogoBG="180" LinearAngleTeamNameBG="180" OffSegmentOpacity="0.1" RoundRadiusNumbers="0" RoundRadiusLogo="0" RoundRadiusTeamName="0" PosSizeStringNumbers="35|8|200|150" PosSizeStringLogo="250|10|150|150" PosSizeStringTeamName="25|165|360|60" TeamNameFontString="Arial|40|True" UseRightSideBanner="False" Style="{av:DynamicResource Scores3DigitControlTemplate}" OverridesDefaultStyle="False" Name="Home3DigitScoreControl" Height="Auto" IsHitTestVisible="True" />
</av:ContentControl>
当我想访问代码背后的控件时,我实际上无法使用重新挂钩到元素
Scores3DigitControl Vis3DigitScoreControlC = (Scores3DigitControl)SBWindowContainerCanvas.FindName("Vis3DigitScoreControl");
if (Vis3DigitScoreControlC == null)
{
MessageBox.Show("Couldn't Find Vis");
}
else
{
Vis3DigitScoreControlC.Visibility = Visibility.Hidden;
}
UI元素在屏幕上显示得很好,但我总是收到一条"找不到Vis"的消息。到目前为止,从我读到的所有内容来看,只要我的元素是画布中某个地方的子元素,就应该使用FindName,对吧?我错过了什么?
感谢
FindName通常用于从模板中获取元素。在您的情况下,您可能最好使用LogicalTreeHelper
LogicalTreeHelper.FindLogicalNode(SBWindowContainerCanvas,
"Vis3DigitScoreControl");
尝试使用该方法:
public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
它应该将您需要的所有对象返回给您。然后你就可以在foreach循环中选择一个了。
尝试使用x:Name而不是Name,并在加载该元素后进行检查。
LogicalTreeHelper.FindLogicalNode
(…)在这里工作,因为它穿过NameScope
边界。
而CCD_ 5在相同的CCD_。
动态添加控件需要对NameScopes
有很好的理解。