How to access the content of my usercontrol
-
19-10-2022 - |
Question
I created a user-control which is a button. When implementing the usercontrol into a parrent control, I'd like to set the icon and tooltip aswell as an enum property that indicates the buttons behaviour.
This is my Button.xaml
<UserControl x:Class="Messenger.Controls.MainWindow.Button"
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"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="30">
<Grid>
<Button Name="btTabButton" Height="30" Width="30">
<Image x:Name="Icon" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Button.ToolTip>
<ToolTip x:Name="Hover">
<TextBlock x:Name="HoverText"/>
</ToolTip>
</Button.ToolTip>
</Button>
</Grid>
And the Button.xaml.cs
namespace Messenger.Controls.MainWindow
{
public partial class Button : UserControl
{
public Button()
{
InitializeComponent();
}
Image img;
public Image Img
{
get { return img; }
set { img = value; this.Icon = img; }
}
string hTxt;
public string HTxt
{
get { return hTxt; }
set { hTxt = value; this.HoverText.Text = hTxt; }
}
}
}
(The function-selection is not implemented yet) Here I call the usercontrol and try to set the property
xmlns:bt="clr-namespace:Messenger.Controls.MainWindow"
<StackPanel Name="spButtons" Height="30" VerticalAlignment="Top" Orientation="Horizontal">
<bt:Button Name="one" >
<bt:Button.HText >
Test
</bt:Button.HText>
</bt:Button>
<bt:Button Name="two" />
<bt:Button Name="tre" />
<bt:Button Name="for" />
</StackPanel>
But I get an error-message:
"The member "HText" is unknown or the member can't be accessed"
//"The attached "HText"-property can't be found in 'Button'" This message does not appear anymore
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow