Можно ли добавить свое свойство в окно или пользовательский элемент в XAMLе по мимо элементов типа texbox и т.п.
1 ответ
Ну, например, так: объявляете UserControl
:
xaml:
<UserControl x:Class="YourNamespace.YourClass" xmlns...
x:Name="ControlRoot">
...
<Expander IsExpanded="{Binding IsExpanded, ElementName=ControlRoot}">
...
</Expander>
...
</UserControl>
xaml.cs:
public partial class YourClass
{
public YourClass()
{
InitializeComponent();
}
#region dependency property bool IsExpanded
public bool IsExpanded
{
get { return (bool)GetValue(IsExpandedProperty); }
set { SetValue(IsExpandedProperty, value); }
}
public static readonly DependencyProperty IsExpandedProperty =
DependencyProperty.Register("IsExpanded", typeof(bool), typeof(YourClass),
new FrameworkPropertyMetadata(true));
#endregion
}
И пользуетесь:
<HierarchicalDataTemplate DataType="{x:Type job:Draft}"
ItemsSource="{Binding Path=WorkList, Mode=TwoWay}">
<code:YourClass IsExpanded="{Binding ...}">
Или можно, по идее, напрямую, без UserControl
'а:
<HierarchicalDataTemplate DataType="{x:Type job:Draft}"
ItemsSource="{Binding Path=WorkList, Mode=TwoWay}">
<Expander IsExpanded="{Binding ...}">
(тут уж как в вашем коде правильнее, вы архитектор, вам решать).
Конкретно для вашей задачи, почему бы не сделать так:
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Style.Triggers>
<DataTrigger Binding="{Binding IsFocused}" Value="False">
<Setter Property="IsExpanded" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
Ну или IsSelected
вместо IsFocused
.
Expander
? И где должно находиться свойство? В вашемUserControl
е? (Что ж из вас информацию вытягивать приходится?)