【问题标题】:Data binding in container user control容器用户控件中的数据绑定
【发布时间】: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


    【解决方案1】:

    试试这个方法

     <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,因此它无法通过 ElementName 搜索逻辑在 Tree 中上下搜索控件来找到

    【讨论】:

    • 我会尝试更新。无论如何,“WindowTitle”(另一个 DP)的部分工作,我猜是因为它不在嵌套控件中。我认为像我的示例这样的嵌套控件不知道“simpleMB”的存在,因为它创建了另一个范围。同时谢谢你!
    猜你喜欢
    • 1970-01-01
    • 2011-02-23
    • 2010-10-10
    • 2012-06-28
    • 1970-01-01
    • 2012-03-29
    • 2013-03-30
    相关资源
    最近更新 更多