Reputation: 155
I have user control like this (e.g.):
<UserControl
x:Class="Tester.UC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Tester"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<TextBlock Name="Name" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="79" Width="380" FontSize="50" TextAlignment="Center"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="152,179,0,0" VerticalAlignment="Top"/>
<TextBox Name="LastName" HorizontalAlignment="Left" Margin="0,109,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="390" Height="65" TextAlignment="Center" FontSize="40"/>
</Grid>
To access this control I have to use Name and then Property. Something liike this.
UC uc = new UC();
uc.LastName.Text = "text";
Is there any way to access it quicker? I mean...
uc.LastName = "Text"
Upvotes: 1
Views: 557
Reputation: 3333
You can declare a property in code-behind to wrap LastName.Text
. For example
public string LastNameText
{
get { return LastName.Text; }
set { LastName.Text = value; }
}
<TextBox Name="LastName" x:FieldModifier="private"/>
Does it worth it? I seriously doubt it.
Upvotes: 1
Reputation: 4296
Not really. LastName is an object of type TextBox, and "Text" is a string. You can't assign a string to a TextBox. TextBox.Text is also of type string, which is why you can assign "Text" to it. Strongly-typed object are a pretty standard principle of C#.
Theoretically you could overload the "=" operator, but this is not advised as it will make your code difficult for others to understand.
Upvotes: 0