我有充当容器的用户控件:
<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/