如何访问我的usercontrol的内容

时间:2014-03-03 15:07:29

标签: c# wpf visual-studio xaml user-controls

我创建了一个用户控件,它是一个按钮。将usercontrol实现为parrent控件时,我想设置图标和工具提示以及指示按钮行为的枚举属性。

这是我的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>

和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; }
    }
}
}

(功能选择尚未实施) 在这里,我调用usercontrol并尝试设置属性

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>

但我收到一条错误消息:

"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

0 个答案:

没有答案