我有一个绑定到递归类的树视图,我想让用户在节点上飞行时添加一个按钮项目(如OneNote:)
我尝试使用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;
}));
}
}
但是使用该代码,我的弹出窗口会不断闪现。
答案 0 :(得分:2)
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>