0
,我有以下的自定义用户控件:如何让我的自定义WPF usercontrol绑定到内容属性?
<UserControl x:Class="TestGUI.TBorder">
<Border Style="{StaticResource brdListBoxItem}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="5" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="0"
Style="{StaticResource tblTitleDataStyle}"
Text="{Binding Header, Mode=OneWay}" />
<Rectangle Grid.Row="1"
Grid.Column="0"
Margin="5,2"
Fill="{StaticResource BlueTextBrush}"
Height="1"
HorizontalAlignment="Stretch" />
<ContentPresenter Name="ccpMain"
Grid.Row="2"
Grid.Column="0"
Content="{Binding Content}" />
<!--<TextBlock Grid.Row="2"
Text="TEST HERE" />-->
</Grid>
</Border>
如果我注释掉“ContentPresenter”,并取消了“TextBlock的”,这似乎符合市场预期。如果我拥有它,那么只有显示的内容是标签内的任何内容。例如:
<local:TBorder Grid.Row="2"
Grid.Column="0" Width="300"
Header="The Header">
<TextBlock Text="astnouhe" />
</local:TBorder>
只显示“TextBlock”。
有人可以向我解释这一点吗?
感谢您的任何帮助。
我明白你在第一段中的意思,但是第二段中的解决方案让我失去了意识。对不起,密集。 – Sako73 2011-03-30 19:09:46
@ Salko73没关系。 :)基本上,你不能只将某些XAML设置为属性,并让WPF呈现控件 - 您需要将实际控件设置为content属性,以便将它们呈现在屏幕上。这有帮助吗? – Andy 2011-03-30 19:10:49
它确实有道理。为了将来的参考,这个链接帮助我将其转化为实际的代码:http://10rem.net/blog/2010/02/05/creating-customized-usercontrols-deriving-from-contentcontrol-in-wpf-4感谢您的帮帮我。 – Sako73 2011-03-30 19:34:56