WPF,请教一个关于UserControl的问题
<UserControl x:Class="WPF1.UserControl1"
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="300" d:DesignWidth="300">
<Grid Background="Red">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Grid>
</UserControl>
<Window x:Class="WPF1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF1" Height="350" Width="600" Name="ddd">
<Grid>
<local:UserControl1 HorizontalAlignment="Left" Content="张三" Height="100" Margin="119,77,0,0" VerticalAlignment="Top" Width="100"/>
</Grid>
</Window>
<m:UserControl1 Width="100" Height="100" Background="Blue" >
<Grid>
</Grid>
</m:UserControl1>
<Grid Background="Red">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Grid>
ContentPresenter会覆盖?在控件模板中经常使用ContentPresenter,怎么没遇到过覆盖哦
你这里用的不是ContentPresenter而是UserControl的Content,为什么你可以在UserControl中写Grid等xaml,正是因为UserControl本身就是一个ContentElement,你赋值当然改写的是UserControl的Content了,而不是UserControl下Grid内的ContentPresenter的Content
你的意思是说,将UserControl的Content重新赋值为"张三"之后,原有的Grid和ContentPresenter其实都没有了,是吗?