Проблема создание компонента интерфейса treeview
Хочу винести в одельный компонет(UserControl) TreeView для использование его. Проблемма с биндингом не отображаються данные из коллекции Сar.
TreeViewFlex-компонент
public partial class TreeViewFlex : UserControl
{
public static readonly DependencyProperty ItemsSourceRootProperty =
DependencyProperty.Register(nameof(ItemsSourceRoot), typeof(IEnumerable), typeof(TreeViewFlex),
new FrameworkPropertyMetadata(ItemsSourceRootProperty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public IEnumerable ItemsSourceRoot
{
get { return (IEnumerable)GetValue(ItemsSourceRootProperty); }
set { SetValue(ItemsSourceRootProperty, value); }
}
public TreeViewFlex()
{
InitializeComponent();
}
}
<TreeView
ItemContainerStyle="{StaticResource TreeViewItemStyle_ExpandAll}"
ItemsSource="{Binding ItemsSourceRoot}"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True">
<e:Interaction.Behaviors>
<behaviours:BindableSelectedItemBehavior SelectedItem="{Binding SelectedCar, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
</e:Interaction.Behaviors>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Width="20" Height="20" Source="{Binding Icon}" />
<Label Grid.Column="1" Content="{Binding Caption}" />
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Как это использую
<Components:TreeViewFlex Grid.Row="3" ItemsSourceRoot="{Binding Car, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="0" Margin="5,0"/>
Ответы (1 шт):
Автор решения: Vladimir
→ Ссылка
<UserControl x:Class="LogicTrain.Components.TreeViewFlex"
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:LogicTrain.Components"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
xmlns:c="clr-namespace:LogicTrain.Common"
xmlns:vm="clr-namespace:LogicTrain.ViewModels"
xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"
xmlns:e="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:behaviours="clr-namespace:LogicTrain.Behaviors"
>
<UserControl.Resources>
<Style x:Key="TreeViewItemStyle_ExpandAll" TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
</Style>
</UserControl.Resources>
<TreeView
ItemContainerStyle="{StaticResource TreeViewItemStyle_ExpandAll}"
ItemsSource="{Binding ItemsSourceRoot, RelativeSource={RelativeSource AncestorType=UserControl}}"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True">
<e:Interaction.Behaviors>
<behaviours:BindableSelectedItemBehavior SelectedItem="{Binding SelectedCar, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
</e:Interaction.Behaviors>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Width="20" Height="20" Source="{Binding Icon}" />
<Label Grid.Column="1" Content="{Binding Caption}" />
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</UserControl>