wpf 自定义控件的应用

ZalmanR 2012-04-09 03:10:32
我自定义了一个控件,然后想在Window中应用这个控件(设置属性),程序提示出错,说找不到属性,能帮忙看看什么原因么?
下面是我的自定义控件,包括一个checkbox,一个textblock。我希望在以后应用的时候可以设置textblock的Text属性:

<UserControl x:Class="Test02.UC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test02"
Height="300" Width="300">
<StackPanel>
<CheckBox x:Name="Chb" Content="Chb_Content"/>
<TextBlock x:Name="Tb" Text="Tb_Content"/>
</StackPanel>
</UserControl>

以下在Window中应用控件:
<Window x:Class="Test02.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300"
xmlns:local="clr-namespace:Test02">
<Grid>
<local:UC Background="LightBlue">
<local:UC.Tb Text="ABC"/>
</local:UC>
</Grid>
</Window>
提示:Error
1 The tag 'UC.Tb' does not exist in XML namespace 'clr-namespace:Test02'. Line 8 Position 14.

请问这是什么原因呢?我在UC这个自定义控件的xaml部分已经定义了Tb啊,我尝试在C#代码部分对Tb进行包装:
public TextBlock TB
{
set{this.Tb = value;}
get{return this.Tb;}
}

    <Grid>
<local:UC Background="LightBlue">
<local:UC.TB Text="ABC"/>
</local:UC>
</Grid>

但是提示:Error 1 Cannot set properties on property elements. Line 8 Position 14.

请问如果我需要在使用自定义控件的时候,设置其属性应该怎么做呢?
谢谢。
...全文
246 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZalmanR 2012-04-09
  • 打赏
  • 举报
回复
就像设置Backgroud一样
ZalmanR 2012-04-09
  • 打赏
  • 举报
回复
哦,可能我定义的方法有问题,我的目的就是,UC这个自定义控件包含一个TextBlock 名字叫Tb(在Xaml中声明的),我想在Window中对UC的实例进行操作,比如将Tb的Text属性设置为"ABC",那应该怎么做呢?
cheng2005 2012-04-09
  • 打赏
  • 举报
回复
<local:UC Background="LightBlue">
<local:UC.Tb Text="ABC"/>
</local:UC>

看了半天,发现Tb是个string.
<local:UC Background="LightBlue" Tb="ABC"/>

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧