wpf - 容器用户控件中的数据绑定(bind)

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

一切都很好,直到我尝试在未来的“InsideContent”中进行数据绑定(bind):

           <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”用户控件不再为人所知)。

我该如何解决这个问题以及为什么会发生这种情况? 谢谢!

最佳答案

试试这个方法

 <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 搜索逻辑是通过在树中上下搜索控件来进行的

关于wpf - 容器用户控件中的数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20574543/

相关文章:

asp.net - 用户控件中的动态 ID 以避免 JavaScript 和 Asp 页面中的重复

c# - 如何让内部控件不覆盖主控件的事件?

c# - 来自源的延迟绑定(bind)

c# - 在 XAML 中查找打开的窗口

c# - WPF 自定义文字环绕

c# - 未能成功地将我的 ViewModel 数据公开给我的 XAML 绑定(bind) View

javascript - 如何防止重新绑定(bind)剑道网格数据项更改?

android - 为什么不在 Android Fragment View 绑定(bind)中使用 lateinit 修饰符?

asp.net - 我们如何使用通用处理程序加载用户控件?

c# - WPF 交互触发器 CallMethodAction