Hi,
with following demo I cannot reproduce your problem. Please, show more details.
XAML Main Window:
<Window x:Class="WpfApp1.MainWindow"
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:WpfApp1"
xmlns:controls="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type controls:MyUserControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CustomObject.IsEnabled}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<CheckBox Content="Visibility" IsChecked="{Binding Path=CustomObject.IsEnabled}"/>
<controls:MyUserControl CustomObject="{Binding Path=CustomObjects[0]}" />
<controls:MyUserControl CustomObject="{Binding Path=CustomObjects[1]}" />
<controls:MyUserControl CustomObject="{Binding Path=CustomObjects[2]}" />
</StackPanel>
</Window>
Classes:
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace WpfApp1
{
class ViewModel
{
public Data1 CustomObject { get; set; } = new Data1();
public Data2 CustomObjects { get; set; } = new Data2();
}
public class Data1 : INotifyPropertyChanged
{
private bool _isEnabled = true;
public bool IsEnabled
{
get => this._isEnabled;
set { this._isEnabled = value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
public class Data2
{
private string[] arr = new string[3] { "test string 1", "test string 2", "test string 3" };
public string this[int i]
{
get { return arr[i]; }
set { arr[i] = value; }
}
}
}
XAML UserControl:
<UserControl x:Class="WpfControlLibrary1.MyUserControl"
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:WpfControlLibrary1"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="800">
<Border x:Name="bo" BorderBrush="Red" BorderThickness="2" Margin="3" Width="100" HorizontalAlignment="Left">
<TextBlock Text="{Binding CustomObject}"/>
</Border>
</UserControl>
CodeBehind UserControl:
using System;
using System.Windows;
using System.Windows.Controls;
namespace WpfControlLibrary1
{
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
this.bo.DataContext = this;
}
public static readonly DependencyProperty CustomObjectProperty =
DependencyProperty.RegisterAttached("CustomObject", typeof(object),
typeof(MyUserControl), new PropertyMetadata(null));
public static object GetCustomObject(DependencyObject obj) => obj.GetValue(CustomObjectProperty);
public static void SetCustomObject(DependencyObject obj, Object value) => obj.SetValue(CustomObjectProperty, value);
}
}
Result: