【发布时间】:2014-01-01 16:42:57
【问题描述】:
我有充当容器的用户控件:
<UserControl x:Class="GUI.Views.Components.Messages.WindowFrame"
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"
x:Name="windowFrame">
<Grid>
<ContentControl Content="{Binding ElementName=windowFrame,Path=InsideContent}">
</Grid>
</UserControl>
下面是代码:
public partial class WindowFrame : UserControl
{
public WindowFrame()
{
InitializeComponent();
}
public object InsideContent
{
get { return (object)GetValue(InsideContentProperty); }
set { SetValue(InsideContentProperty, value); }
}
// Using a DependencyProperty as the backing store for InsideContent. This enables animation, styling, binding, etc...
public static readonly DependencyProperty InsideContentProperty =
DependencyProperty.Register("InsideContent", typeof(object), typeof(WindowFrame), new PropertyMetadata(null));
}
一切都很好,直到我尝试在未来“InsideContent”中进行数据绑定:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:GUI.ViewModels.Converters"
xmlns:local="clr-namespace:GUI.Views.Components.Messages" x:Class="GUI.Views.Components.Messages.SimpleMessageBox"
Title="SimpleMessageBox" Height="202" Width="438"
x:Name="simpleMB" AllowsTransparency="True" WindowStyle="None" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
<Window.Resources>
<converters:MessageTypeToMessageBoxPictureConverter x:Key="MessageTypeToMessageBoxPictureConverter"/>
</Window.Resources>
<Grid>
<local:WindowFrame WindowTitle="{Binding ElementName=simpleMB,Path=MessageBoxTitle}">
<local:WindowFrame.InsideContent>
<Grid>
<!-- Data binding here fails! -->
<Image Stretch="None" Source="{Binding ElementName=simpleMB,Path=MessageType,Converter={StaticResource MessageTypeToMessageBoxPictureConverter}}"/>
<TextBlock Margin="20,10,0,111" Text="{Binding ElementName=simpleMB,Path=MessageBoxMessage}" VerticalAlignment="Center" TextWrapping="Wrap"/>
<!---->
</Grid>
</local:WindowFrame.InsideContent>
</local:WindowFrame>
<!-- Data binding here works! -->
<TextBlock Margin="10,138,10,48" Text="{Binding ElementName=simpleMB,Path=MessageBoxMessage}" VerticalAlignment="Center" TextWrapping="Wrap"/>
<Image Stretch="None" Source="{Binding ElementName=simpleMB,Path=MessageType,Converter={StaticResource MessageTypeToMessageBoxPictureConverter}}" Margin="42,30,199,39"/>
</Grid>
就像在“InsideContent”(网格)中,“simpleMB”用户控件不再为人所知)。
我该如何解决这个问题,为什么会这样? 谢谢!
【问题讨论】:
标签: wpf data-binding user-controls