问题
I have the following xaml and I am trying to get to the Grid to make it invisible at some point in my code if the code reaches there. Being new to .net and silverlight and xaml I am not sure why my code behind doesn't find Grid Controlpanel
please see my xaml code here
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:smf="clr-namespace:Microsoft.SilverlightMediaFramework.Core;assembly=Microsoft.SilverlightMediaFramework.Core" xmlns:smfm="clr-namespace:Microsoft.SilverlightMediaFramework.Core.Media;assembly=Microsoft.SilverlightMediaFramework.Core" xmlns:plugins3d="clr-namespace:Microsoft.SilverlightMediaFramework.Plugins.Primitives.S3D;assembly=Microsoft.SilverlightMediaFramework.Plugins" xmlns:Microsoft_SilverlightMediaFramework_Utilities_Offline="clr-namespace:Microsoft.SilverlightMediaFramework.Utilities.Offline;assembly=Microsoft.SilverlightMediaFramework.Utilities" xmlns:System="clr-namespace:System;assembly=mscorlib" xmlns:smf_util="http://schemas.microsoft.com/smf/2010/xaml/converters" xmlns:smf_accesscontrols="http://schemas.microsoft.com/smf/2010/xaml/accesscontrols" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" xmlns:ec="http://schemas.microsoft.com/expression/2010/controls"
x:Class="SPlayer.MainPage"
d:DesignHeight="300" d:DesignWidth="400" mc:Ignorable="d">
<UserControl.Resources>
................
................
<Style x:Key="SMFPlayerStyle1" TargetType="smf:SMFPlayer">
<Setter Property="Background" Value="Black"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="Template">
.......
......
<Grid x:Name="ControlPanelGrid" Margin="8,-26,8,13" Grid.Row="1" Grid.RowSpan="4" Opacity="0">
<Rectangle x:Name="ControlPanel" Margin="2,3,3,5" Stroke="#FF474747" RadiusY="4" RadiusX="4" Opacity="0.9" StrokeThickness="0.5">
<Rectangle.Effect>
<DropShadowEffect Opacity="0.26" BlurRadius="7" Direction="319"/>
</Rectangle.Effect>
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF191919" Offset="0.936"/>
<GradientStop Color="#FF454545" Offset="0.187"/>
<GradientStop Color="#FF191919"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Grid x:Name="timelinecontrols" Grid.ColumnSpan="1" Margin="0,0,165,13" d:LayoutOverrides="GridBox" d:IsHidden="True">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<smf:Timeline x:Name="TimelineElement" Cursor="Hand" Chapters="{TemplateBinding Chapters}" EndPosition="{TemplateBinding EndPosition}" Foreground="{x:Null}" HorizontalContentAlignment="Stretch" IsLive="{TemplateBinding IsMediaLive}" LivePosition="{TemplateBinding LivePosition}" Margin="80,0,170,-10" StartPosition="{TemplateBinding StartPosition}" TimelineMarkers="{TemplateBinding TimelineMarkers}" VerticalAlignment="Center" Style="{StaticResource TimelineStyle1}" />
<Border x:Name="TimeContainer" BorderThickness="1" Grid.Column="1" HorizontalAlignment="Left" Margin="0" Opacity="0">
<Grid Height="28" MaxHeight="28" MinWidth="60" MinHeight="28" UseLayoutRounding="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.25*"/>
<ColumnDefinition Width="0.367*"/>
<ColumnDefinition Width="0.383*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="CurrentPositionElement" Foreground="#FFC1C1C1" FontSize="9" HorizontalAlignment="Left" Padding="8,0,0,0" TextAlignment="Center" Text="{Binding PlaybackPosition, Converter={StaticResource TimeSpanValueConverter}, RelativeSource={RelativeSource TemplatedParent}}" VerticalAlignment="Center"/>
<TextBlock x:Name="TimeSeparatorElement" Grid.Column="1" Foreground="#FFFDFAFA" FontSize="9" HorizontalAlignment="Center" Margin="3,0" Opacity="0.4" Text="|" VerticalAlignment="Center"/>
<TextBlock x:Name="CurrentDurationElement" Grid.Column="2" Foreground="#FFFDFDFD" FontSize="9" HorizontalAlignment="Left" Opacity="0.4" Padding="0,0,8,0" Text="{Binding EndPosition, Converter={StaticResource TimeSpanValueConverter}, RelativeSource={RelativeSource TemplatedParent}}" VerticalAlignment="Center"/>
</Grid>
</Grid>
</Border>
</Grid>
<Image x:Name="btnVloume" Margin="0,-1,383,5" Source="volume.png" Stretch="Fill" Height="49" d:LayoutOverrides="Width, GridBox" Width="53" HorizontalAlignment="Right" d:IsHidden="True"/>
<smf:VolumeControl x:Name="VolumeElement" VolumeLevel=".5" RenderTransformOrigin="0.5,0.5" Margin="0,-46.5,326.074,-50.5" UseLayoutRounding="False" d:LayoutRounding="Auto" HorizontalAlignment="Right" Width="36" Style="{StaticResource VolumeControlStyle1}" d:IsHidden="True" >
<smf:VolumeControl.RenderTransform>
<CompositeTransform Rotation="90.02"/>
</smf:VolumeControl.RenderTransform>
</smf:VolumeControl>
<ToggleButton x:Name="FullScreenToggleElement" BorderThickness="0" Cursor="Hand" HorizontalAlignment="Right" HorizontalContentAlignment="Stretch" Height="28" Margin="0,14,153,11" Padding="0" Style="{StaticResource FullScreenElementStyle}" VerticalContentAlignment="Stretch" Width="29" d:LayoutOverrides="HorizontalAlignment" Opacity="0"/>
<Image x:Name="_3D" HorizontalAlignment="Right" Margin="0,1,180,3" Source="3D.png" Stretch="Fill" Width="53" Height="49" Cursor="Hand" Opacity="0.8" MouseLeftButtonUp="_3D_MouseLeftButtonUp">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<ei:ChangePropertyAction x:Name="_3d_100_Opacity" PropertyName="Opacity" Value="1"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeave">
<ei:ChangePropertyAction x:Name="_3d_80_Opacity" PropertyName="Opacity" Value="0.8"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
<Image x:Name="analytics" HorizontalAlignment="Left" Margin="980,3,0,-1" Source="analytics.png" Stretch="Fill" Width="53" Cursor="Hand" Opacity="0.7">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<ei:ChangePropertyAction x:Name="analytics_MouseEnter" PropertyName="Opacity" Value="1"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeave">
<ei:ChangePropertyAction x:Name="analytics_MouseLeave" PropertyName="Opacity" Value="0.7"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
<Image x:Name="btn_fullscreen" HorizontalAlignment="Right" Margin="0,0,140,4" Source="full_screen.png" Stretch="Fill" Width="53" Height="49" Opacity="0.8" Cursor="Hand">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<ei:ChangePropertyAction x:Name="btn_fullscreen_mouseOver" PropertyName="Opacity" Value="1"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeave">
<ei:ChangePropertyAction x:Name="btn_fullscreen_mouseOut" PropertyName="Opacity" Value="0.8"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
<smf:PlayElement x:Name="PlayElement" Content="PlayElement" PlayState="{TemplateBinding PlayState}" HorizontalAlignment="Left" Height="51" Margin="9,2,0,0" Style="{StaticResource PlayElementStyle1}" Width="53" Cursor="Hand" />
<Image Margin="0,4,5,8" Source="logo.png" Stretch="Fill" HorizontalAlignment="Right" Width="96"/>
</Grid>
<Grid x:Name="ControllerContainer" Height="40" Grid.Row="4" Visibility="{Binding IsControlStripVisible, Converter={StaticResource BoolToVisibilityConverter}, RelativeSource={RelativeSource TemplatedParent}}" Opacity="0">
<Grid.Background>
<LinearGradientBrush EndPoint="0.481481,1" StartPoint="0.481481,0.00636957">
<GradientStop Color="#FF78808B" Offset="0"/>
<GradientStop Color="#FF58606A" Offset="0.287958"/>
<GradientStop Color="#FF38424E" Offset="0.680628"/>
<GradientStop Color="#FF333D49" Offset="0.984293"/>
</LinearGradientBrush>
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid x:Name="playercontrols" Grid.ColumnSpan="1" Margin="19,0,16,0" Opacity="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button x:Name="ReplayElement" BorderThickness="0" HorizontalAlignment="Right" Height="28" Margin="0" Style="{StaticResource ReplayButtonStyle}" Width="36"/>
<Button x:Name="RewindElement" BorderThickness="0,1,0,0" Grid.Column="1" HorizontalAlignment="Right" Height="28" Margin="0" Style="{StaticResource RWButtonStyle}" Width="36">
<Button.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFA3AEB9" Offset="0"/>
<GradientStop Color="#FF8399A9" Offset="0.375"/>
<GradientStop Color="#FF718597" Offset="0.375"/>
<GradientStop Color="#FF617584" Offset="1"/>
</LinearGradientBrush>
</Button.BorderBrush>
</Button>
<smf:PlayElement Cursor="Hand" Grid.Column="2" Margin="0" PlayState="{TemplateBinding PlayState}" Width="60"/>
<Button x:Name="FastForwardElement" BorderThickness="0" Grid.Column="3" HorizontalAlignment="Right" Height="28" Margin="0" Style="{StaticResource FFButtonStyle}" Width="36"/>
<ToggleButton x:Name="SlowMotionElement" Grid.Column="4" HorizontalAlignment="Right" Height="28" Margin="0" Style="{StaticResource SlowMotionButtonStyle}" ToolTipService.ToolTip="Slow Motion" Width="36"/>
</Grid>
<Grid x:Name="divider1" Grid.Column="1" HorizontalAlignment="Left" Width="2">
<Path Data="M245.16667,0 L245.16667,39.109009" HorizontalAlignment="Left" Stretch="Fill" UseLayoutRounding="False" Width="1">
<Path.Stroke>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0.509"/>
<GradientStop Color="#FF343E4A" Offset="1"/>
<GradientStop Color="#FF757D87" Offset="0"/>
</LinearGradientBrush>
</Path.Stroke>
</Path>
<Path Data="M245.16667,0 L245.16667,39.109009" HorizontalAlignment="Left" Margin="1,0,0,0" Stretch="Fill" UseLayoutRounding="False" Width="1">
<Path.Stroke>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFDFEFF" Offset="0.509"/>
<GradientStop Color="#FF36404C" Offset="1"/>
<GradientStop Color="#FF757D87" Offset="0"/>
</LinearGradientBrush>
</Path.Stroke>
</Path>
</Grid>
<Grid x:Name="divider2" Grid.Column="3" HorizontalAlignment="Left" Width="2" Opacity="0">
<Path Data="M245.16667,0 L245.16667,39.109009" HorizontalAlignment="Left" Stretch="Fill" UseLayoutRounding="False" Width="1">
<Path.Stroke>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0.509"/>
<GradientStop Color="#FF343E4A" Offset="1"/>
<GradientStop Color="#FF757D87" Offset="0"/>
</LinearGradientBrush>
</Path.Stroke>
</Path>
<Path Data="M245.16667,0 L245.16667,39.109009" HorizontalAlignment="Left" Margin="1,0,0,0" Stretch="Fill" UseLayoutRounding="False" Width="1">
<Path.Stroke>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFDFEFF" Offset="0.509"/>
<GradientStop Color="#FF343E4A" Offset="1"/>
<GradientStop Color="#FF757D87" Offset="0.009"/>
</LinearGradientBrush>
</Path.Stroke>
</Path>
</Grid>
<Grid x:Name="functioncontrols" Grid.ColumnSpan="1" Grid.Column="4" Margin="16,0" Opacity="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ToggleButton x:Name="CaptionToggleElement" Cursor="Hand" Content="CC" Grid.Column="0" HorizontalAlignment="Right" Height="28" Margin="0" Style="{StaticResource CaptionDisplayToggleButtonStyle}" ToolTipService.ToolTip="Closed Captions" Width="36" d:LayoutOverrides="GridBox"/>
<ToggleButton x:Name="GraphToggleElement" Cursor="Hand" Content="G" Grid.Column="1" Height="28" Style="{StaticResource PlayerGraphButtonStyle}" ToolTipService.ToolTip="Toggle Graph" Width="36"/>
<ComboBox x:Name="AudioStreamSelectionElement" BorderBrush="{StaticResource btnBaseGradient}" Background="Black" Cursor="Hand" Grid.ColumnSpan="1" Grid.Column="3" Foreground="Black" HorizontalAlignment="Right" Height="28" ItemsSource="{TemplateBinding AvailableAudioStreams}" Margin="0" Style="{StaticResource AudioStreamMenu}" SelectedItem="{TemplateBinding SelectedAudioStream}" Width="110">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Foreground="{StaticResource iconGradient}" Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
<Grid x:Name="divider3" Grid.Column="5" HorizontalAlignment="Left" Height="40" Margin="0" Width="2" Opacity="0">
<Path Data="M245.16667,0 L245.16667,39.109009" HorizontalAlignment="Left" Stretch="Fill" UseLayoutRounding="False" Width="1">
<Path.Stroke>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0.509"/>
<GradientStop Color="#FF343E4A" Offset="1"/>
<GradientStop Color="#FF757D87" Offset="0"/>
</LinearGradientBrush>
</Path.Stroke>
</Path>
<Path Data="M245.16667,0 L245.16667,39.109009" HorizontalAlignment="Left" Margin="1,0,0,0" Stretch="Fill" UseLayoutRounding="False" Width="1">
<Path.Stroke>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFDFEFF" Offset="0.509"/>
<GradientStop Color="#FF343E4A" Offset="1"/>
<GradientStop Color="#FF757D87" Offset="0"/>
</LinearGradientBrush>
</Path.Stroke>
</Path>
</Grid>
<smf:BitrateMonitor x:Name="BitrateMonitorElement" Grid.Column="6" HighDefinitionBitrate="{TemplateBinding HighDefinitionBitrate}" Height="40" Margin="5,0,7,0" MaximumPlaybackBitrate="{TemplateBinding MaximumPlaybackBitrate}" PlaybackBitrate="{TemplateBinding PlaybackBitrate}" Width="36" Opacity="0"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<smf:SMFPlayer x:Name="smfPlayer" Style="{StaticResource SMFPlayerStyle1}" PlaylistVisibility="Disabled"/>
</Grid>
</UserControl>
here is my code behind file
namespace SPlayer
{
public partial class MainPage : UserControl
{
.........
public MainPage(StartupEventArgs e)
{
......
as you can see my namespace is correct in xaml but I still cant access the grid <Grid x:Name="ControlPanelGrid" Margin="8,-26,8,13" Grid.Row="1" Grid.RowSpan="4" Opacity="0">
thanks
回答1:
In your C# code you can use the FindChild<T>
found in this SO question
It would look something like:
Grid controlPanelGrid = UIHelper.FindChild<Grid>(smfPlayer, "ControlPanelGrid");
回答2:
For clarification: your Grid ControlPanelGrid
is a child of SMFPlayerStyle1
in the resources of your UserControl
?
<UserControl.Resources>
<Style x:Key="SMFPlayerStyle1" TargetType="smf:SMFPlayer">
[...]
<Grid x:Name="ControlPanelGrid" Margin="8,-26,8,13" Grid.Row="1" Grid.RowSpan="4" Opacity="0">
[...]
</Style>
</UserControl.Resources>
To access this element programatically, you need to dive down the object graph starting with SMFPlayerStyle1...
. Depending on structure of the SMFPlayer
object you may be able to access it through a property of this.
来源:https://stackoverflow.com/questions/8582246/how-to-get-to-grid-xname-controlpanelgrid