Developer
Developer

Reputation: 4321

CustomControl TextBox is inaccesible

I'm developing Windows 8 Tablet application

This is my custom user control:

<Grid>
    <TextBox x:Name="Points" Visibility="Collapsed" IsHitTestVisible="False" Grid.ColumnSpan="3" Grid.Column="4" HorizontalAlignment="Left" Margin="48,10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" TextChanged="TextBox_TextChanged"/>
</Grid>

I placed on the MainPage form:

<local:MyUserControl1 x:Name="Test" HorizontalAlignment="Left" Margin="280,493,0,0" VerticalAlignment="Top" Width="584" Height="166"/>

In C# (MAinPage.xaml.cs):

Test.Points.Text = "";

Error:

Gamification.MyUserControl1.Points' is inaccessible due to its protection level

In Windows Phone 8 i didn't have those problems...

How to solve it?

EDIT 2:

Control was added: Right click -> Add -> New item -> User control

Full Control XAML:

<UserControl
x:Class="Gamification.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Gamification"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="200"
d:DesignWidth="800">

<Grid>
    <TextBox x:Name="Points" Visibility="Collapsed" IsHitTestVisible="False" Grid.ColumnSpan="3" Grid.Column="4" HorizontalAlignment="Left" Margin="48,10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" TextChanged="TextBox_TextChanged"/>
</Grid>
</UserControl>

Full Control C#:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace Gamification
{
public sealed partial class MyUserControl1 : UserControl
{
    public MyUserControl1()
    {
        this.InitializeComponent();
    }

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {

    }
}
}

Main page XAML:

<Page
x:Class="Gamification.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Gamification"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <local:MyUserControl1 x:Name="Test" HorizontalAlignment="Left" Margin="280,493,0,0" VerticalAlignment="Top" Width="584" Height="166"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="670,692,0,0" VerticalAlignment="Top" Click="Button_Click"/>
</Grid>
</Page>

Main Page C#:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Test.Points.Text = "";
    }

Upvotes: 0

Views: 473

Answers (2)

Benoit Catherinet
Benoit Catherinet

Reputation: 3345

You can add x:FieldModifier="public" on your TextBlock to make it available outside the user control.
Just keep in mind that it will not be a really good practice as it will break encapsulation of your UserControl (in your case the UserControl should expose a PointText dependency property so that the page using the control don't need to know how this text is shown, using a dependency property will also enable you to use binding for this property).

Upvotes: 0

Tim Heuer
Tim Heuer

Reputation: 4301

Your UserControl is a type itself. It will only expose those elements that are public for itself. The x:Name'd elements within your control are only accessible to itself unless you expose them public to the UserControl.

You would need to expose a property that allows the consumer of the UserControl to set the property which would, in turn, change your internal element. This is commonly done through a DependencyProperty and PropertyChange notification on that DP.

Upvotes: 2

Related Questions