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

罗姆·西里

我有充当容器的用户控件:

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

我该如何解决这个问题,为什么会发生呢?谢谢!

悠洲

这样尝试

 <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中上下搜索控件而无法找到它

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

在项目控件中对用户控件进行数据绑定

来自分类Dev

数据绑定和用户控件

来自分类Dev

用户控件与数据的绑定属性

来自分类Dev

WPF 数据绑定用户控件

来自分类Dev

使用模板10的UWP用户控件中的数据绑定

来自分类Dev

WPF:自定义创建的用户控件中的数据绑定

来自分类Dev

使用 c#/WinForms 将数据绑定到重复用户控件中的控件

来自分类Dev

数据绑定控件中的评估

来自分类Dev

数据绑定到定期用户控件

来自分类Dev

在嵌套用户控件中绑定DependencyProperty

来自分类Dev

在用户控件中定义命令绑定

来自分类Dev

将用户控件绑定到itemcontrol中

来自分类Dev

在项目控件中获取用户控件绑定到的对象?

来自分类Dev

WPF数据将ViewModel属性绑定到用户控件内部的ListBox中,背后的代码

来自分类Dev

父页面数据控件绑定之后,用户控件中的下一个事件是什么?

来自分类Dev

控件渲染器中的数据绑定

来自分类Dev

GridView控件中的复杂数据绑定

来自分类Dev

从控件的数据绑定中获取 EditValue

来自分类Dev

WPF用户控件数据绑定不起作用

来自分类Dev

使用MVVM ViewModel的WPF用户控件数据绑定

来自分类Dev

具有MVVM和用户控件的WPF数据绑定

来自分类Dev

具有MVVM和用户控件的WPF数据绑定

来自分类Dev

将用户控件绑定到数据上下文的实例?

来自分类Dev

自定义用户控件不绑定数据

来自分类Dev

在嵌套用户控件之间绑定数据

来自分类Dev

如何在紧凑框架中创建用户容器控件?

来自分类Dev

在WPF中的用户控件之间传递数据

来自分类Dev

如何在WPF中绑定用作DataGridTemplateColumn的用户控件失败

Related 相关文章

热门标签

归档