容器用户控件中的数据绑定

时间:2013-12-13 19:34:07

标签: wpf data-binding user-controls

我的用户控件就像容器一样:

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

一切都很有效,直到我将来尝试进行数据绑定&#34; InsideContent&#34;:

           <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>

在&#34; InsideContent&#34; (网格),&#34; simpleMB&#34;用户控制不再为人所知。)

我该如何解决这个问题?为什么会这样? 谢谢!

1 个答案:

答案 0 :(得分:2)

以这种方式试试

 <ContentControl  Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},Path=InsideContent}" />

<local:WindowFrame WindowTitle="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}},Path=MessageBoxTitle}">

 <Image Stretch="None" Source="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}},Path=MessageType,Converter={StaticResource MessageTypeToMessageBoxPictureConverter}}"/>

 <TextBlock Margin="20,10,0,111" Height="30"  Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}},Path=MessageBoxMessage}" VerticalAlignment="Center" TextWrapping="Wrap"/>

我认为elementName不起作用因为local:WindowFrame.InsideContent不是UIElement,因此无法通过在Tree中上下搜索控件来找到ElementName搜索逻辑