Здравствуйте, igor-booch, Вы писали:
...
IB>RelativeSource Self я полностью убрал из кода. Дело не в этом.
Для пояснения пример:
UserControl без CodeBehind:
<UserControl x:Class="WpfApplication1CS.Window18UC1"
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"
xmlns:local="clr-namespace:WpfApplication1CS"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel>
<Button ContentTemplate="{Binding ItemTemplateVM}"/>
</StackPanel>
</UserControl>
XAML-Window:
<Window x:Class="WpfApplication1CS.Window18"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1CS"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Name="window"
Title="Window18" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="dt">
<Label Content="{Binding Data.Name1, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
</DataTemplate>
</Window.Resources>
<StackPanel>
<local:Window18UC1/>
<Button ContentTemplate="{StaticResource dt}" Name="btn"/>
</StackPanel>
</Window>
CodeBehind-Window:
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
namespace WpfApplication1CS
{
/// <summary>
/// Interaction logic for Window18.xaml
/// </summary>
public partial class Window18 : Window
{
public Window18()
{
InitializeComponent();
this._data = new Window18Data() { Name1 = "Текст на кнопке" };
this.ItemTemplateVM = btn.ContentTemplate;
}
private DataTemplate _itemTemplateVM;
public DataTemplate ItemTemplateVM
{
get { return _itemTemplateVM; }
set
{
_itemTemplateVM = value;
OnPropertyChanged();
}
}
private Window18Data _data;
public Window18Data Data
{
get { return _data; }
set
{
_data = value;
OnPropertyChanged();
}
}
#region OnPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propname = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propname));
#endregion
}
public class Window18Data
{
public string Name1 { get; set; }
}
}