TreeView добавить элемент с помощью мыши - PullRequest
0 голосов
/ 24 апреля 2018

У меня есть древовидное представление, привязанное к рекурсивному классу, и я хотел бы позволить пользователю добавлять элемент с помощью кнопки, когда он пролетает над узлом (например, OneNote: OneNote exemple)

Я пытался с этим кодом в файле xaml:

<TreeView Grid.Row="1" 
            BorderBrush="{x:Null}"
            ItemsSource="{Binding Law.RootGroup.Groups}">
    <i:Interaction.Behaviors>
        <functions:BindableSelectedItemBehavior SelectedItem="{Binding SelectedGroup, Mode=TwoWay}" />
    </i:Interaction.Behaviors>
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate DataType="{x:Type law:BimFluxGroup}"
                                    ItemsSource="{Binding Groups}">

            <Grid Margin="0,0,2,0" Background="Transparent">
                <Grid.RowDefinitions>
                    <RowDefinition SharedSizeGroup="LawRowHeight"/>
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding Name}" VerticalAlignment="Center">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseEnter">
                            <i:InvokeCommandAction 
                                Command="{Binding ShowPopUpCommand, 
                                            RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
                                <i:InvokeCommandAction.CommandParameter>
                                    <MultiBinding Converter="{StaticResource MultiConverter}">
                                        <Binding RelativeSource="{RelativeSource AncestorType={x:Type TextBlock}}" />
                                        <Binding />
                                    </MultiBinding>
                                </i:InvokeCommandAction.CommandParameter>
                            </i:InvokeCommandAction>
                        </i:EventTrigger>
                        <i:EventTrigger EventName="MouseLeave">
                        <i:InvokeCommandAction 
                            Command="{Binding CloseToolTipCommand, 
                                            RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
                    </i:EventTrigger>
                    </i:Interaction.Triggers>
                </TextBlock>
            </Grid>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

и с этим в файле cs:

private ICommand _showPopUpCommand;
public ICommand ShowPopUpCommand
{
    get
    {
        return _showPopUpCommand ?? (_showPopUpCommand = new RelayCommand<object>(obj =>
        {
            if (!(obj is object[] values)) return;
            if (!(Resources["AddGroup"] is Popup)) return;
            if (!(values[1] is BimFluxGroup group)) return;

            _popup = (Popup)Resources["AddGroup"];
            _popup.DataContext = group;
            _popup.PlacementTarget = values[0] as UIElement; ;
            _popup.StaysOpen = false;
            _popup.IsOpen = true;
        }));
    }
}

Но с этим кодом мое всплывающее окно постоянно мигает.

1 Ответ

0 голосов
/ 24 апреля 2018

user1672994 дал мне хорошие советы! Я добавил свою кнопку добавления в шаблон и связал видимость с помощью мыши на TreeView ItemTemplate.

<TreeView.ItemTemplate>
    <HierarchicalDataTemplate DataType="{x:Type law:BimFluxGroup}"
                    ItemsSource="{Binding Groups}">
        <Grid Margin="0,0,2,0" Background="Transparent">
            <Grid.RowDefinitions>
                <RowDefinition SharedSizeGroup="LawRowHeight"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <TextBlock Text="{Binding Name}" VerticalAlignment="Center"/>
            <Button x:Name="AddGroup" Grid.Row="1" Visibility="Collapsed" HorizontalAlignment="Right">
                <TextBlock Text="Add Group" FontSize="10"/>
            </Button>
        </Grid>
        <HierarchicalDataTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="AddGroup" Property="Visibility" Value="Visible"/>
            </Trigger>
        </HierarchicalDataTemplate.Triggers>
    </HierarchicalDataTemplate>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...