У меня есть свойство зависимости dpvalid в моем usercontrol view1 & view2
Код: Выделить всё
Код: Выделить всё
< /code>
public partial class View1: UserControl
{
public static readonly DependencyProperty DpValidProperty=
DependencyProperty.Register(nameof(DpValid), typeof(bool), typeof(View1), new PropertyMetadata(true, OnChanged));
public ListPropertiesView()
{
InitializeComponent();
}
public bool DpValid
{
get { return (bool)GetValue(DpValidProperty); }
set { SetValue(DpValidProperty, value); }
}
private static void OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is View1 control)
{
control.UpdateTemplateSelector();
}
}
private void UpdateTemplateSelector()
{
DataTemplateSelector ts = DpValid
? (DataTemplateSelector)Resources["DT1"]
: (DataTemplateSelector)Resources["DT2"];
treeview.ItemTemplateSelector = ts;
}
}
< /code>
Its working. But I am getting some binding error when I switch from [b]View1[/b] to [b]View2[/b]
HorizontalContentAlignment TreeViewItem.HorizontalContentAlignment HorizontalAlignment Cannot find source: RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1'.
But I am not setting these properties.
Another observation I made is that when the content control switches to [b]View2[/b], the Dependency Property value change is triggered with the default value, even though the bound property has not been modified.
It appears that during this value change, we attempt to set the ItemTemplateSelector
Подробнее здесь: https://stackoverflow.com/questions/792 ... hides-from