Home > DeveloperSection > Forums > Access a UserControl from code in WPF app
Jayden Bell
Jayden Bell

Total Post:105

Points:739
Posted on    December-16-2013 12:22 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 1088  View(s)
Rate this:

I have a created a usercontrol in my wpf application. I have used it like so:

<Page x:Class="InstallerToolkit.Pages.PageVideosNvr"

  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

  xmlns:my="clr-namespace:MyProject.UserControls"

  mc:Ignorable="d"

  d:DesignHeight="525" d:DesignWidth="1050"

Title="PageVideos">

  <Grid>

        <my:UserControlVideoPlayer Name="VideoPlayer" Margin="559,74,35,155">

    </my:UserControlVideoPlayer>

</Grid>

Now in my C# page I want to access it but the VideoPlayer object doesnt appear when I type its name in the code behind c# page.

What  I do to access it as I want to set one of its properties.


Pravesh Singh

Total Post:411

Points:2881
Posted on    December-16-2013 12:46 AM

Hi Jayden,

Give x:Name to your UserControl instead of Name

<my:UserControlVideoPlayer x:Name="VideoPlayer" Margin="559,74,35,155">

It will be accessible now in code behind using this.VideoPlayer.

I would suggest to make it a thumb rule that always use x:Name whenever referring to elements in XAML.


Don't want to miss updates? Please click the below button!

Follow MindStick